1

I have an activity A that passes data to activity B through Intent. Now Activity B calls Activity C and Activity C calls Activity D. Now Activity D is calling activity B. Now activity B check for the intent data but the Activity D doesn't pass any data.

Now one way to do this is to pass data through C and D and finally get it in B but i would like to know if there is a better way to handle this ?

i would like to add that the data can be an object with a lot of variables and array.

Edit :- The data is necessary for activity B to work.

Thanks in advance.

ANUJ GUPTA
  • 905
  • 13
  • 22
  • if your are passing same data to many activities, Use Sharedpreference instead of intent.putextra() – Shashwat Gupta Aug 10 '17 at 11:42
  • Hi @ANUJ GUPTA, its simple way to store data on following manner like, 1) Use SharedPreferences 2)Store in getter setter like pojo class concept. – jack Aug 10 '17 at 11:43
  • use can create and activity with `android:launchMode="singleTop"` to ActivityB then it will open again the same instance which called C – MadScientist Aug 10 '17 at 11:43
  • The data i am passing can be an object so Shared Preference cannot be used. Here is a link to support this https://stackoverflow.com/questions/5418160/store-and-retrieve-a-class-object-in-shared-preference – ANUJ GUPTA Aug 10 '17 at 11:50
  • yes in shared preffrence you can also use objects usin library compile 'com.google.code.gson:gson:2.7' – Shashwat Gupta Aug 10 '17 at 11:55
  • save them as Global Object, check my answer [here](https://stackoverflow.com/questions/45188214/how-to-maintain-data-of-five-activities-when-comes-to-first-activity-on-click-of/45188685#45188685) – nKalai Aug 10 '17 at 12:02

5 Answers5

1

Use SharedPreferrence;

For storing values;

SharedPreferences sp=getSharedPreferences("Login", Context.MODE_PRIVATE);
SharedPreferences.Editor Ed=sp.edit();
Ed.putString("id",login_id.getText().toString() );
Ed.putString("password",login_password.getText().toString() );
Ed.commit();

For getting values;

SharedPreferences sharedPreferences = getSharedPreferences("Login", MODE_PRIVATE);
String uName = sharedPreferences.getString("id", "");
String password = sharedPreferences.getString("password", "");
Vishal Vaishnav
  • 3,346
  • 3
  • 26
  • 57
1

You can Check Your Intent is not null and its has Extras value or not by getting Bundle and another check if Bundle oBject is not null then you check keys are there or not in Bundle Object.

Intent intent = getIntent();
 if(intent!=null)
 {
   Bundle bundle =  intent.getExtras();
   if(bundle!=null)
   {
      if(bundle.containsKey("key"))
      {
      //here you can put your code for Actvity B when comes from Activity  A.
      }
   }
 } 

EDIT 1 Use singleTask launchMode in Activity B in manifest file.

android:launchMode="singleTask" 
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
1

You can use gson.jar to store class objects into SharedPreferences.

add GSON dependency in Gradle file:

compile 'com.google.code.gson:gson:2.7'

Creating a shared preference:

SharedPreferences  mPrefs = getPreferences(MODE_PRIVATE);

To save:

  Editor prefsEditor = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(MyObject);
prefsEditor.putString("MyObject", json);
prefsEditor.commit();

To Retrieve:

    Gson gson = new Gson();
String json = mPrefs.getString("MyObject", "");
MyObject obj = gson.fromJson(json, MyObject.class);
Shashwat Gupta
  • 876
  • 9
  • 22
1

If the data you are passing is different, then assign a variable (named activity_name) with the name of the activity that is calling the intent. In this way, you can check if you are coming to activity B from activity A or D.

Let me try to explain with an example:

ActivityA

String activity_name = "ACTIVITY_A";

Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("calling_activity", activity_name);
intent.putExtra("your_data", your_data); 
startActivity(intent);

ActivityB

Intent intent = getIntent();
String callingActivity = intent.getExtras().getString("calling_activity");

if (callingActivity.matches("ACTIVITY_A")) {
    // Do something
} else if (callingActivity.matches("ACTIVITY_D")) {
    // Do something else
}

and in ActivityD

String activity_name = "ACTIVITY_D";

Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("calling_activity", activity_name);
// No passed data in Actiivty D
startActivity(intent);

This same logic can be employed using SharedPreferences as remarked in other answers and comments.

Ajil O.
  • 6,562
  • 5
  • 40
  • 72
0

write below code in your activity B.

String str= getIntent().getStringExtra("key");

if (str == null){
str= "";
}
Praful Patel
  • 209
  • 3
  • 12