如何返回到previous片段活动 [英] how to return to previous fragment activity
问题描述
三个片段活动: MainFragmentActivity
,报告
和 ReportsType
。
报告呼吁 ReportsType
。
有在 ReportsType
后退按钮具有以下code回去:
公共无效goBackReport(视图V)
{
完(); //为什么MainFragmentActivity.java在完成之后()叫什么名字?
}
但它总是返回 MainFragmentActivity.java
。但它应该是 Reports.java
。
更新:
Reports.java
公共无效showReport(视图v){
。字符串标记= v.getTag()的toString();
意图I5 =新意图(这一点,ReportsType.class);
i5.putExtra(名,标签);
FragmentTransactiontransaction = getSupportFragmentManager()调用BeginTransaction()。
transaction.addToBackStack(空).commit();
startActivity(I5); }
ReportsType.java
公共无效goBackReport(视图V)
{
getSupportFragmentManager()popBackStack()。 //什么都没发生
}
有没有呼叫的概念完成()
在片段
。相反,你应该执行交易时保持碎片堆。例如:
ft.addToBackStack(NULL); //英尺是FragmentTransaction
所以,当你preSS 返回键后,当前的活动(持有多个片段)会加载previous片段,而不是结束本身。
Three fragment activities: MainFragmentActivity
, Reports
and ReportsType
.
Reports is calling ReportsType
.
There is a back button in ReportsType
to go back with the following code:
public void goBackReport(View v)
{
finish(); // why MainFragmentActivity.java is called after finish()?
}
But its always returning to MainFragmentActivity.java
. But it should be Reports.java
.
UPDATE:
Reports.java
public void showReport(View v) {
String tag = v.getTag().toString();
Intent i5 = new Intent(this, ReportsType.class);
i5.putExtra("name", tag);
FragmentTransactiontransaction=getSupportFragmentManager().beginTransaction();
transaction.addToBackStack(null).commit();
startActivity(i5);
}
ReportsType.java
public void goBackReport(View v)
{
getSupportFragmentManager().popBackStack(); //nothing happens
}
There's no concept of calling finish()
on Fragment
. Instead, you should keep stack of fragments when performing transactions. For example:
ft.addToBackStack(null); // ft is FragmentTransaction
So, when you press back-key, the current activity (which holds multiple fragments) will load previous fragment rather than finishing itself.
这篇关于如何返回到previous片段活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!