sjain
sjain

Reputation: 23344

how to return to previous fragment activity

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
    }

Upvotes: 0

Views: 5942

Answers (2)

waqaslam
waqaslam

Reputation: 68167

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.

Upvotes: 2

Mit Bhatt
Mit Bhatt

Reputation: 1645

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

Upvotes: 0

Related Questions