简体   繁体   中英

“If the previous activity was … do something”

I'm developping an Android Application but I'm blocked. I'm searching for a condition like this :

if the previous activity was ...{
       *do something*
}

I want to fill an Arraylist of Strings only if the String comes from the previous activity.

Thank you in advance for your answers !

V.

You can use Shared Preferences to mark the previous activity:

In on create method of all activities mark the curr activity name in the Shared Preferences and then it's only do the consult.

You can see how to use shared preferences here

Hey you can put data in the intent you use to start this activity. So you can put a different identifier depending on wich "previous" activity started this one. like so :

Intent intent = new Intent(getBaseContext(), NextActivity.class);
intent.putExtra("PREVIOUS_ACTIVITY", this.getClass().getSimpleName());
startActivity(intent)

then you can retrieve this information by doing : then in the NextActivity, retrieve the values in onCreate() :

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String previous activity= extras.getString("PREVIOUS_ACTIVITY");
}

Then you can test the variable and do wathever you want. Hope this help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM