简体   繁体   English

如果活动是从另一个活动开始的,则启动方法

[英]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. 无论如何,这里是:假设我有2个活动,A和B。活动B是我的应用程序启动器活动,因此,当我启动我的应用程序时,活动B是第一次运行。 From there, i'm going to start activity A via an intent. 从那里,我将通过意图开始活动A。 Now, I'm in activity A and starting activity B again via an intent. 现在,我处于活动A,并通过意图再次开始活动B。 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? 现在,通过活动A的意图启动了活动B,我想运行活动B内的showStuff()方法。如何?

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 启动B时通过一个布尔标志“ showStuff”

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

And in B in onCreate 并在onCreate中的B中

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

also in B override onNewIntent 在B中也重写onNewIntent

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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