问题
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
}
回答1:
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.
回答2:
Try this..
Intent i=new Intent(this,MainFragmentActivity.class)
startActivity(i);
finish();
Intent i=new Intent(this,Reports.class)
startActivity(i);
finish();
Intent i=new Intent(this,ReportsType.class)
startActivity(i);
finish();
When you call new Activity write finish() in last
来源:https://stackoverflow.com/questions/15897502/how-to-return-to-previous-fragment-activity