Android 如何恢复活动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1427136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:04:55  来源:igfitidea点击:

How to resume an activity?

android

提问by Diablo.Wu

Suppose there are three activity,the display order is a->b->c. Now, I would like to resume the Activity a directly,after Activityc complete some operations. Resume the Activitya by Activityc directly,but create a new instance of Activity a.

假设有3个activity,显示顺序是a->b->c。现在,我想直接恢复Activity a,在Activityc完成一些操作后。直接Activity通过Activityc恢复a ,但是新建一个Activity a的实例。

回答by dylan murphy

as @Matthias said, do you want to resume or restart Activity a? if you want to restart it then you would include FLAG_ACTIVITY_CLEAR_TASKin your intent which does this: "If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started".

正如@Matthias 所说,您是要恢复还是重新启动 Activity a?如果你想重新启动它,那么你会FLAG_ACTIVITY_CLEAR_TASK在你的意图中包括:“如果在传递给 Context.startActivity() 的意图中设置,这个标志将导致与活动相关联的任何现有任务在活动开始”

or you could probably just call finish();in Activity a's onPause

或者你可以只调用finish();Activity a'sonPause

if you want to resume it then you just call startActivity(new Intent(Parent.this, ActivityA.class));or whatever your code is

如果你想恢复它,那么你只需调用startActivity(new Intent(Parent.this, ActivityA.class));或任何你的代码

additional information: Activities, Intent, Activity and Task Design

附加信息:活动意图活动和任务设计

回答by user1097540

  • In b you use BroadcastReceiverin onResume()method with content finish()as:
  • 在 b 中,您BroadcastReceiveronResume()方法中使用内容finish()为:

BroadcastReceiver receiverFinish=null;

@Override

protected void onResume() {

  super.onResume();
  if(receiverFinish!=null){
      unregisterReceiver(receiverFinish);
  }
  receiverFinish=new BroadcastReceiver() {

广播接收器接收器Finish=null;

@Override

protected void onResume() {

  super.onResume();
  if(receiverFinish!=null){
      unregisterReceiver(receiverFinish);
  }
  receiverFinish=new BroadcastReceiver() {
 @Override          
public void onReceive(Context context, Intent intent) {


                if(intent.getAction().equals("finish.activity")){
                        MyActivity.this.finish();
                    }
                                }       };
                registerReceiver(receiverFinish, new IntentFilter("finish.activity"));

    }
  • Then in c you call :
  • 然后在 c 中调用:

//finish b activity

Intent intent=new Intent("finish.activity"); sendBroadcast(intent);

//finish c activity

finish();

//完成b活动

意图意图=新意图(“完成。活动”);发送广播(意图);

//完成c活动

结束();

  • Hope useful for you! good cluck!
  • From Pham Trung Phuong - Vsoft Corp.
  • 希望对你有用!好咯咯!
  • 范忠PHUONG - Vsoft公司