N.Preusche
N.Preusche

Reputation: 37

Back Button to go another Activity not parent Activity in Android

I created a back button with this code

    getActionBar().setHomeButtonEnabled(true);
    getActionBar().setDisplayHomeAsUpEnabled(true);

So that the user comes back to the parent Activity. But now I want that the user comes back to another Activity (not the parent activity). How can I do that ?

Upvotes: 1

Views: 1965

Answers (4)

Osman Goni Nahid
Osman Goni Nahid

Reputation: 1279

Add this in manifest, for your activity name modify it

        <activity
            android:name="SecondActivity"
            android:parentActivityName="ParentActivity" >
            <!-- Parent activity meta-data to support 4.0 and lower -->
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.app_name.A" />
        </activity>

Then you should add code like below in your Child or Second Activity no need to pass intent just finish() current activity like below

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {

            case android.R.id.home:

                    Intent intent = new     Intent(SecondActivity.this,ParentActivity.class);
                    startActivity(about);
                    finish();
                return true;

            default:
                return super.onOptionsItemSelected(item);
        }

Upvotes: 0

Sathish Kumar J
Sathish Kumar J

Reputation: 4335

Try this way,

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_file, menu);
    return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case android.R.id.home:

                Intent about = new Intent(MainActivity.this,Target.class);;
                startActivity(about);
                finish();

            return true;

        default:
            return super.onOptionsItemSelected(item);
    }

}

here android.R.id.home refer to your Back icon which is in ActionBar

Upvotes: 0

Sohail Zahid
Sohail Zahid

Reputation: 8149

Handle actionbar home button pressed event and performed your logic

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
                switch (item.getItemId()) {
                case android.R.id.home:
                      // Your desired class
                       startActivity(new Intent(ThisActivity.this, NextActivity.class)); 
                    break;
                }
                return true;
        }

Upvotes: 2

Harshad Pansuriya
Harshad Pansuriya

Reputation: 20910

You will have to override onBackPressed() from your activity:

@Override
public void onBackPressed()
{
    super.onBackPressed(); 
    startActivity(new Intent(ThisActivity.this, NextActivity.class));
    finish();

}

Note : In this code ThisActivity is your current Activity and NextActivity is which you open Activity on Back Button Click.

Upvotes: 4

Related Questions