完成当前活动从片段 [英] Finishing current activity from a fragment

查看:102
本文介绍了完成当前活动从片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我使用的导航抽屉的活动片段。它包含点击时启动新活动按钮(startActivity从片段只需在当前活动调用startActivity)。

I have a fragment in an activity that I am using as a navigation drawer. It contains buttons that when clicked start new activities (startActivity from a fragment simply calls startActivity on the current activity).

有关我的生活中,我似乎无法找出我将如何开始新的一前一后完成当前的活动。

For the life of me I can't seem to figure out how I would finish the current activity after starting a new one.

我期待实现像这样的片段:

I am looking to achieve something like this in the fragment:

@Override
public void onClick(View view) {
    // TODO Auto-generated method stub
    if (view == mButtonShows) {
        Intent intent = new Intent(view.getContext(), MyNewActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    } 
}

但似乎Fragment.class没有实现完成()(就像它实现startActivity(...))。

But it seems Fragment.class does not implement finish() (like it implements startActivity(...)).

我想,当他们推出第二个活动backstack清除活动。 (所以pressing的新活动将回技术上把它们回发射器)

I would like the activity backstack cleared when they launch the 2nd activity. (so pressing back from the new activity would technically drop them back to the launcher)

推荐答案

在与碎片的工作,而不是使用或指的情况下,一定要使用 getActivity()。你应该叫

When working with fragments, instead of using this or refering to the context, always use getActivity(). You should call

getActivity().finish();

从片段完成你的活动。

to finish your activity from fragment.

这篇关于完成当前活动从片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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