简体   繁体   中英

Starting a method if activity is started from another activity

Hi!

I'm about to ask a really dumb question, but i assure you that i've searched the web and either there is no answer (highly unlikely) or i've come across a solution but have been unable to recognize it.

Anyway, here it is: Let's say i have 2 activities, A and B. Activity B is my applications launcher activity, so when I start my app, the activity B is first run. From there, i'm going to start activity A via an intent. Now, I'm in activity A and starting activity B again via an intent. Now, having started activity B via an intent from the activity A, i want to run the method showStuff() that's inside activity B. How?

Sorry for the weird story, i'm unfortunately unable to express myself in techincal language. Thank you very much for help!

Pass a boolean flag "showStuff" though the intent when start B

Intent intent = new Intent(this, B.class);
intent.putExtra("showStuff", true);
startActivity(intent);  

And in B in onCreate

Intent intent = getIntent();
if (intent != null) {
    boolean showStuff = intent.getBooleanExtra("showStuff", false);
    if (showStuff) {
         showStuff();
     }
}  

also in B override onNewIntent

@Override
protected void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);

    if (intent != null) {
        boolean showStuff = intent.getBooleanExtra("showStuff", false);
        if (showStuff) {
             showStuff();
         }
    }  
}

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