11

我有一个具有这种转换的应用程序:

 A -> B -> C -> D-> C

进入后C,我必须检查一个标志。然后我必须将它作为意图(让我们说intentX = false)传递给D. 在 中做某事后,按下按钮后D会返回。C我所做的只是再次传递intentX值为true,然后再次传递startActivity C。所以发生的事情是它创建了另一个活动 C。

我想要发生的是我不必开始一个新的活动 C,而是通过调用来使用以前的super.onBackPressed()C。但我无法传递intentX. 还有其他方法可以实现我想要的。我可能错过了一些。

4

2 回答 2

14

你想要的是startActivityForResult(). 当你去 from Cto 时D,不要使用startActivity()use 来代替startActivityForResult()。然后,当您想返回时DC请使用setResult()which 可以包含一个Intent对象extras以传递回C.

onBackPressed()如果您不必这样做,我不建议您这样做,因为这不是用户所期望的。相反,您应该通过点击等事件返回此数据Button

所以,C你会做类似的事情

 Intent i = new Intent(new Intent(C.this, D.class);
 startActivityForResult(i, 0);

然后在D您准备返回时

 Intent i = new Intent();
 i.putExtra();  // insert your extras here
 setResult(0, i);

然后当您返回时,C您将输入此方法(取自文档

protected void onActivityResult(int requestCode, int resultCode,
         Intent data) {
     if (requestCode == PICK_CONTACT_REQUEST) {
         if (resultCode == RESULT_OK) {
             // A contact was picked.  Here we will just display it
             // to the user.
             startActivity(new Intent(Intent.ACTION_VIEW, data));

             /* 
                can also get the extra sent back through data
                using data.getStringExtra("someKey"); 
                assuming the extra was a String
             */

         }
于 2013-08-14T22:42:37.653 回答
1

There are some cases where startActivityForResult is not really needed or it is not practical to change all startActivity calls for startActivityForResult.

If the simple case of just starting a previous activity 'again' is needed, my recommendation is: Use the FLAG_ACTIVITY_CLEAR_TOP flag.

Quoting a brief description:

If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.

So this example

   // From ActivityD
   Intent intent = new Intent(getApplicationContext(), ActivityB.class);
   intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // The flag we wanted
   intent.putExtra(ActivityB.SOME_EXTRA_THAT_I_NEED_CHANGED, SomeValue); // Example of changing the intent to get something new..
   startActivity(intent);

Where you will get that new intent is defined by which launch mode and which flags where used to start it (in this case our ActivityB).

The currently running instance of activity B in the above example will either receive the new intent you are starting here in its onNewIntent() method, or be itself finished and restarted with the new intent. If it has declared its launch mode to be "multiple" (the default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same intent, then it will be finished and re-created; for all other launch modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be delivered to the current instance's onNewIntent().

于 2015-09-09T14:15:17.203 回答