user3534088
user3534088

Reputation: 11

Action bar back button from un activity to previous activity fragment

I'm using a navigation drawer in a main activity with many fragments

For example : I have fragment 1 for presentation And fragment2 that contains a listView. If I click an item in the listView it will launch another activity When i click the action bar back button i want to go to the main activity fragment2

But the fragment1 is used

How can I use the second fragment

Thanks for your answers

Upvotes: 0

Views: 1253

Answers (2)

user3534088
user3534088

Reputation: 11

I have added the code below in my second activity :

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
onBackPressed();
        return true;
    }

It's working as I expected

is it a clean solution ?

Thanks

Upvotes: 1

Orphamiel
Orphamiel

Reputation: 884

In your second activity's onCreate, put(although i think you already have) :

getActionBar().setDisplayHomeAsUpEnabled(true);

and inside second activity's options handling(i think you've done this too) :

@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
    switch (item.getItemId()) 
    {
    case android.R.id.home:
            //perhaps use intent if needed but i'm sure there's a specific intent action for up you can use to handle
            finish();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

and inside your first activity's onCreate's intent handler for example :

if(Intent.ACTION_VIEW.equals(action))

you set the current page of the viewpager with viewpager.setCurrentItem(position). If you want to change the page for different items you could use intent.putExtra() in your second activity and then retrieve it in your first activity.

Upvotes: 2

Related Questions