从Activity返回一个片段 [英] Get back to a fragment from an Activity

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

问题描述

在活动C中我有三个片段,他们表现为制表符。我必须从一个片段去一个新的活动X.现在我想回到从活动X片段。



我已经覆盖onBackPressed,但不知道如何从Activity返回fragment / not fragmentActivity。



如果我想回到一个活动到另一个活动,我重新回到按下,并使用intent调用这个活动..



我想做一些这样的事情。这段代码是为了回到以前的活动。

  @Override 
public void onBackPressed()
{

Intent intent = new Intent(CurrentActivity.this,ActivityYouLikeToGo.class);

intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

startActivity(intent);
}

在此先感谢。

解决方案

您可以添加一个TAG(基本上为片段的字符串名称),然后使用 findFragmentByTag()加载片段。



用于 findFragmentByTag()的Google文档是 here。



或者你也可以 addToBackStack( ),同时从 FragmentTransaction ,doc链接这里
from google docs:


通过调用addToBackStack(),替换事务被保存到
返回栈所以用户可以通过按Back按钮来反转交易并返回
前一个分段。



I have three fragment in an Activity C. they are behaving as tabs. I have to go from a fragment to a new Activity X. Now i want to come back to fragment from Activity X.

I have override onBackPressed but don't know how to go back to fragment/not fragmentActivity from an Activity.

if i want to go back to an Activity to another activity i override on back pressed and using intent call that actvity..

i want to do some thing like this .. this code is for coming back to previous activity

@Override
public void onBackPressed()
{

    Intent intent = new Intent(CurrentActivity.this,ActivityYouLikeToGo.class);

    intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

    startActivity(intent);
}

thanks in Advance..

解决方案

You can add a TAG (A string name for the fragment basically) and then load the fragment using findFragmentByTag().

Google documentation for findFragmentByTag() is here.

Or you can also addToBackStack() while calling the fragment from FragmentTransaction, doc link here. from google docs:

"By calling addToBackStack(), the replace transaction is saved to the back stack so the user can reverse the transaction and bring back the previous fragment by pressing the Back button."

这篇关于从Activity返回一个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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