need to open home activity on back pressed android-ThrowExceptions

Exception or error:

I have a activity stack in normal case is A -> B -> C and when i received a push notification and have implemented click_action it opens Activity C which is correct.

Now, what is need is when activity C opens from push notification after pressing back button it gets closed and whole application goes in background, but i need to navigate to Home screen (Activity A)

<activity
    android:name="Activity_A"
    android:exported="false"
    android:screenOrientation="sensorPortrait"
    android:theme="@style/MyAppTheme"
    android:windowSoftInputMode="stateHidden|adjustResize" />
<activity
    android:name="Activity_B"
    android:exported="false"
    android:screenOrientation="sensorPortrait"
    android:theme="@style/MyAppTheme"
    android:windowSoftInputMode="stateHidden|adjustResize" />
<activity
    android:name="Activity_C"
    android:exported="false"
    android:screenOrientation="sensorPortrait"
    android:theme="@style/MyAppTheme"
    android:windowSoftInputMode="adjustPan">
    <intent-filter>
        <action android:name="Activity_C" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
How to solve:

It happens because usually previous activities are saved in a stack and when user press back button the stack is called so from what i understood from your question if You want to open Home Activity through Activity C just call intent from Activity C to Home Activity by overriding the onbackpressed() method in Activity C.
Here is an Example..

Intent homeIntent = new Intent(this,HomeActivity);
startActivity(homeIntent);
finish();

finish() is used for removing the stacked activities.

###

you can add below line in you AndroidManifest.xml file in Activity C.

android:launchMode="singleTop"

It will redirect you to home activity when opened from Notification and is back pressed. Hope this helps you.

###

When you create the Notification, add both FLAG_ACTIVTY_NEW_TASK and FLAG_ACTIVTY_CLEAR_TASK to the Intent before creating the PendingIntent and the Notification.

This will cause ActivityC to be started in a new task, clearing out any previous activities that might have been there. Upon BACK press in ActivityC, the app will have no active activities, which will retun the user to the HOME screen. Starting the app again will begin with ActivityA.

This will have no effect on the normal progression of activities from A->B->C.

Leave a Reply

Your email address will not be published. Required fields are marked *