如何活动Ç返回到Android的活性的?活性、Android

2023-09-12 23:44:15 作者:难拥友

让说,有三种活性

A-> B-> C

如果我从C到B回来,我所要做的仅仅是:

Android iconify 使用详解

在C:

  onBack pressed();
 

和B中

 保护无效的onSaveInstanceState(包outState){
.....
}

如果(savedInstanceState!= NULL){
.....
}
 

以上code将处理为从C到B但是,如果C到A,我应该怎么做呢?此外,如何处理,如果我想A和C之间的所有活动都destoryed当C到A?谢谢

解决方案   

另外,如何处理,如果我想A和C之间的所有活动都destoryed当C到A?

使用该意图国旗

 意向书我=新的意图(ActivityC.this,ActivityA.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(ⅰ);
 

  

但是,如果C到A,我该怎么办?

此标志将保留所有活动之间的堆栈,但带来的正面

 意向书我=新的意图(ActivityC.this,ActivityA.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(ⅰ);
 

在这两种情况下,你会想完成()在C调用后打电话到 startActivitiy()如果你想把它删除。

您可以找到所有意图旗的在这里,在文档

Let say there are three activity

A->B->C

If I am returning from C to B , What I have to do is simply:

In C:

            onBackPressed();

And in B

protected void onSaveInstanceState(Bundle outState) {
.....
}

if (savedInstanceState != null) {
.....
}

The above code will handle the case for From C to B. However, what if C to A, what should I do? Also, how to handle if I would like all activities between A and C are destoryed when C to A? Thanks

解决方案

Also, how to handle if I would like all activities between A and C are destoryed when C to A?

Use this Intent Flag

Intent i = new Intent(ActivityC.this, ActivityA.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);

However, what if C to A, what should I do?

This flag will keep all Activities in between on the stack but bring A to the front

Intent i = new Intent(ActivityC.this, ActivityA.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);

In either case, you will want to call finish() on C after calling startActivitiy() if you want it removed.

You can find all Intent Flags Here in the Docs