简体   繁体   English

返回MainActivity并添加来自另一个Activity的数据

[英]Return to MainActivity with added data from another Activity

I know how to send data from an activity to the Next Activity : [ Intent.putExtra(key, value); 我知道如何将数据从活动发送到Next Activity :[ Intent.putExtra(key, value); ]. ]。

I also know how to return from an activity to the previous one and include data: [ see this stackoverflow answer ]. 我也知道如何从一个活动返回到上一个活动,并包括数据:[ 请参见stackoverflow答案 ]。

What I want to know however is, how I can return to the Home Activity and include data. 但是,我想知道的是如何返回家庭作业并包含数据。 As an example: 举个例子:

  1. I start the app in Activity A and do some stuff 我在活动A中启动该应用程序并做一些事情
  2. Then I go to Activity B and do some stuff 然后我去活动B做一些事情
  3. Then I go to Activity C and do some stuff 然后我去活动C做一些事情
  4. And then I want to return to Activity A (and do different stuff) 然后我想回到活动A(并做其他事情)

In step 4 I want to do something different than normally in the onResume (or another method it returns to), so the data I include is a boolean with a key (preferably using intent.putExtra("myKey", true); (or false if something failed)). 在第4步中,我想做一些与onResume(或它返回的其他方法)中的正常操作不同的事情,因此,我包含的数据是带有键的布尔值(最好使用intent.putExtra("myKey", true); (或如果失败,则返回false)。 In the onResume I then do something like: 然后在onResume中执行以下操作:

@Override
protected void onResume(){
    super.onResume();
    if(intent.getExtras() != null && intent.getAction().toString().equals("myKey")){
        if(intent.getExtras().getBoolean("myKey")){
            // do something else (step 4) on success
        }
        else{
            // do something else (step 4) on fail
        }
    }
    else{
        // Do regular stuff I normally do in the onResume()
    }
}

Ok, I think I've have solved my problem. 好的,我想我已经解决了我的问题。 My HomeActivity already is a BroadcastReceiver, so I just send an intent from my last Activity to my HomeActivity using the Broadcast.. 我的HomeActivity已经是一个BroadcastReceiver,所以我只是使用广播将意图从最后一个Activity发送到我的HomeActivity。

\n

Will test this to see if it works. 将对此进行测试以查看其是否有效。 Though I'm kinda doubting it will when I'm in another Activity. 虽然我有点怀疑当我参加另一项活动时会否。 (My HomeActivity is already a BroadcastReceiver for some Http-requests I've had to send and needed the result of, which are AsyncTasks of course.) (我的HomeActivity已经是我必须发送的一些Http请求的广播接收器,并且需要它的结果,它们当然是AsyncTasks。)

Ok, this doesn't work when I'm not already in the MainActvity. 好的,当我不在MainActvity中时,这将不起作用。 So more suggestions are welcome.. 因此欢迎更多建议。

Ok, I've found a solution (also thanks to shimi_tab's answer and Budius comment about onNewIntent): 好的,我找到了一个解决方案(也要感谢shimi_tab的回答BudiusonNewIntent的 评论 ):

In my Activity C when I want to return: 在我想返回的活动C中:

Intent home = new Intent(this, A.class);
home.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
home.putExtra("myKey", true);
startActivity(home);

In my Activity A: 在我的活动A中:

// This allows us to use getIntent to get the latest intent, instead of the first Intent used in the onCreate
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);
}

@Override
protected void onResume(){
    super.onResume();

    if(getIntent().getExtras() != null && getIntent().getExtras().getBoolean("myKey")){
        // Do something else after returning from C
    }
    else{
        // Do regular things on a normal onResume (like back from B or Settings)
    }
}

NOTE (trivial to the original question): In my case I use Google Services in my Activity A, where I had googleApiClient.onConnect(); 注意(与原始问题googleApiClient.onConnect(); ):在我的情况下,我在活动A中使用了Google Services,其中我有googleApiClient.onConnect(); in the onStart() method. onStart()方法中。 So, I also had to add the following to the onConnected() method: 因此,我还必须将以下内容添加到onConnected()方法中:

@Override
public void onConnected(Bundle b){
    Bundle extras = getIntent().getExtras();
    if(extras == null || (extras != null && !extras.getBoolean("myKey"))){
        // Do regular stuff
    }
    else{
        // Do something else after we returned from C
    }
}

to put data back to the calling activity, you'll use this method: 要将数据放回调用活动,您将使用以下方法:

public final void setResult (int resultCode, Intent data)

on this intent you feel free to fill it up to all the data you need. 根据这种意图,您可以随意填写所需的所有数据。 The activity result method is as follows: 活动结果方法如下:

protected void onActivityResult (int requestCode, int resultCode, Intent data)

so that's the same data that you put on the result. 因此,您输入的结果就是相同的数据。

You should make activity "A" state less. 您应减少活动“ A”状态。 call Activity "A" again with a new intent with the flag FLAG_ACTIVITY_CLEAR_TOP. 使用标志FLAG_ACTIVITY_CLEAR_TOP的新意图再次调用活动“ A”。

Intent intent = new Intent(this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

And all the data you want. 以及您想要的所有数据。

You shouldn't relay on ActivityResult for what you are trying to do. 您不应该在ActivityResult上进行中继。

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

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