Jacky So
Jacky So

Reputation: 233

Launch Activity in parent activity

I would like to launch an activity(TargetActivity) all the way back from some activity(MainOptionActivity) so that if the user press the back button from the TargetActivity, it goes back to the MainOptionActivity.

Right now its doing the following

SplashScreenActivity -> MainOptionActivity -> SomeActivity -> ... -> SomeService -> TargetActivity

But I want to do this, which would discards every activities/services after MainOptionActivity

SplashScreenActivity -> MainOptionActivity -> TargetActivity

Thanks!

P.S The application is running at API 15, parentActivityName is not support

Upvotes: 0

Views: 273

Answers (2)

Hbibna
Hbibna

Reputation: 568

try doing this :

for each activity that you don't want to go back to when you press the back button add this bit of code to the intent you use to call the activity:

Intent intent = new Intent(this, YourActivity.class);
contactIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

This flag tells your activity to not keep it's history in the activities stack.

Upvotes: 0

Alejandro Cumpa
Alejandro Cumpa

Reputation: 2363

You can declare the parent of the activity from the manifest like this:

 <activity
            android:name=".activities.TargetActivity"
            android:parentActivityName=".activities.MainOptionActivity" >
            <!-- Parent activity meta-data to support 4.0 and lower -->
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".activities.MainOptionActivity" />
 </activity>

UPDATE:

I know it is not the better way, but how about overriding the call of the back button?

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
        {
            Log.d("MainActivity", "onKeyDown");
            if (keyCode == KeyEvent.KEYCODE_BACK)
                {
                    Intent i = new Intent(getApplicationContext(), TargetActivity.class);
                    MainOptionActivity.this.finish();
                    startActivity(i);   
            }
        else
            {
                return super.onKeyDown(keyCode, event);
            }
    }

Upvotes: 1

Related Questions