返回堆栈中的特定活动 [英] Go back to specific activity from stack

查看:115
本文介绍了返回堆栈中的特定活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行文件管理器之类的操作.在操作栏中,我想像"Google Drive"中那样进行文件夹导航应用程序.我需要创建方法,该方法可以按数字从末尾转到上一个活动.

I'm trying to do something like file manager. And in action bar I wanna to do folder navigation like in "google drive" app. I need create method which can go to previous activity by number from end, or something like this.

示例:

因此,如果我有堆栈:[1]-> [2]-> [3]-> [4]-> [5]

So if I have stack: [1] -> [2] -> [3] -> [4] -> [5]

我需要走到第二位:所以我需要从堆栈中删除[3],[4]和[5],然后转到[2].

And I need go to second: so I need delete [3], [4], and [5] from stack and go to [2].

所有活动都是ContentActivity.java一类.

All activities is one class ContentActivity.java.

怎么可能?

更新:

一些代码如何开始活动:

Some code how I start activities:

public class ContentActivity extends Activity implements AdapterView.OnItemClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        Intent intent = getIntent();
        String folderToOpen = intent.getStringExtra("folderName");
        fillList(folderToOpen);
    }


    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        ...
        Intent intent = new Intent(ContentList.this, ContentList.class);
        intent.putExtra("folderName", item.getName());
        startActivity(intent);
    }
}

推荐答案

假定Activity2是您要进行的第二项活动,

Assuming that Activity2 is the second activity that you want to go,

尝试一下:

Intent intent = new Intent(this,Activity2.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

根据 Android文档关于FLAG_ACTIVITY_CLEAR_TOP

According to Android Documentation about FLAG_ACTIVITY_CLEAR_TOP

如果已设置,并且正在启动的活动已经在 当前任务,而不是启动该任务的新实例 活动,除此以外的所有其他活动将被关闭, 该意图将作为 新的意图.

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.

这篇关于返回堆栈中的特定活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆