Skip to content Skip to sidebar Skip to footer

How I Can Passing Data From One Activity To Multiple Activities?

I want to send these data from current activity to more 'BusInformationsCard' activity. @Override public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int positio

Solution 1:

you can use shared preference to use your data in all over project. You just need to create an App Preference class like this:-

publicclassAppPrefrences {

            privatestaticSharedPreferences mPrefs;
            privatestaticSharedPreferences.Editor mPrefsEditor;

            publicstaticbooleanisUserLoggedOut(Context ctx) {
                mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
                return mPrefs.getBoolean("id_logged_in", true);
            }

            publicstaticvoidsetUserLoggedOut(Context ctx, Boolean value) {
                mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
                mPrefsEditor = mPrefs.edit();
                mPrefsEditor.putBoolean("id_logged_in", value);
                mPrefsEditor.commit();
            }

    publicstaticStringgetUserName(Context ctx) {
            mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
            return mPrefs.getString("userName", "");
        }

        publicstaticvoidsetUserName(Context ctx, String value) {
            mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
            mPrefsEditor = mPrefs.edit();
            mPrefsEditor.putString("userName", value);
            mPrefsEditor.commit();
        }

publicstaticvoidclearAllPreferences(Context ctx) {
        mPrefs = PreferenceManager.getDefaultSharedPreferences(ctx);
        mPrefsEditor = mPrefs.edit();
        mPrefsEditor.clear();
        mPrefsEditor.commit();
    }
        }

and now just call these methods for save data and get saved data. create a your own methods for save data and get saved data

Solution 2:

You can get your data from one activity to another using Intent's getExtra method

Here is example ::

Example

String name;

name = getIntent().getStringExtra("driverName");

Solution 3:

If you wish to pass the complete ticket object, you can use Serializable or Parcelable class in Java.

These classes help you to convert your object in a form which can be transferred between activities.

All you need to do in case of Serializable is to extend your ticket class

publicclassTicketextendsSerializable {}

In case of Parcelable, you need to add a little bit more code (in case of Java).

publicclassTicketextendsParcelable {

   publicvoidwriteToParcel(Parcel dest, int flags) {
      dest.writeString(name);
      dest.writeString(busLine);
      // Similarly for all the other parameters.
   }

   publicstaticfinal Parcelable.CreatorCREATOR=newParcelable.Creator() {
    public Ticket createFromParcel(Parcel in) {
        returnnewTicket(in);
    }
    public Ticket[] newArray(int size) {
        returnnewTicket[size];
    }
 };

}

In both these cases, now you can directly pass your complete ticket object in the intent just like intent.putExtra("ticket", ticket); and can receive it in the other activity like, Ticket ticket = getIntent().getSerializableExtra("ticket")*/ getParcelable */ if you have extended Parcelable.

The main difference between Parcelable and Serializable is the speed difference, Parcelable is faster than Serializable. Also, parcelable is customisable, and hence developers have the independence to play with their data. Also Serializable used Java Reflection API, and hence ends up with a lot of garbage object during the conversion.

Post a Comment for "How I Can Passing Data From One Activity To Multiple Activities?"