简体   繁体   English

通过关闭所有堆叠的活动来开始新活动

[英]start a new activity by closing all the stacked activities

My activity stack: 我的活动堆栈:

Main -> Restaurant Menu -> Order Details.

When order is placed I need to close the Restaurant Menu and Order Details and Start new activity called Active Order 下订单后,我需要关闭“ 餐厅”菜单和“订单详细信息”,并启动名为“活动订单”的新活动

Main -> Active Order.  (need to close top 2 activity)

I have tried with all the options like below 我已经尝试了以下所有选项

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |  Intent.FLAG_ACTIVITY_SINGLE_TOP);

All the solution I found behaving like below 我发现的所有解决方案如下

  1. Main -> Restaurant Menu -> Active Order. 主菜单->餐厅菜单->有效订单。 (close Order details) (关闭订单详细信息)
  2. only Active Order. 仅活动订单。 (close everything and start new) (关闭所有内容并重新开始)

How can I achieve this. 我该如何实现。 Main -> Active Order. 主菜单->有效订单。 ?

Thanks. 谢谢。

My idea that you could use is to set a SharedFile each time a new intent starts, and after you use startActivity(); 我的想法是,每次启动新的意图时以及使用startActivity();之后,都要设置一个SharedFile startActivity(); , I would put finish(); ,我会把finish(); after to close the current activity before it opens the new one. 之后关闭当前活动,然后再打开新活动。

I have used bellow like.. 我用过波纹管

    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    this.startActivity(intent);
    this.finish();

You can launch an activity like that: 您可以启动这样的活动:

 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  

This is a minimal example how you can create an Intent and start an Activity in Android. 这是一个最小示例,说明如何在Android中创建Intent并启动Activity。

Intent intent = new Intent(this,Home.class); 
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(intent); 
 Intent i = new Intent(getActivity(), ActiveOrder.class);
                        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(i);

Try this way 试试这个

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

According to Android Developers resource on Intent: 根据有关Intent的Android开发者资源

  • FLAG_ACTIVITY_CLEAR_TASK : 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. FLAG_ACTIVITY_CLEAR_TASK :如果在传递给Context.startActivity()Intent进行设置,则此标志将导致在启动活动之前清除与该活动关联的任何现有任务。
  • FLAG_ACTIVITY_NEW_TASK : If set, this activity will become the start of a new task on this history stack. FLAG_ACTIVITY_NEW_TASK :如果设置,此活动将成为此历史记录堆栈上新任务的开始。

After calling startActivity(intent) put this line for better accuracy: 调用startActivity(intent)之后, startActivity(intent)下行放进去以提高准确性:

YourActivity.this.finish();

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

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