how to return to previous fragment activity

笑着哭i 提交于 2019-12-25 00:17:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!