back stack navigation when opening the app from a widget is not working

187 Views Asked by At

I want to launch the DetailActivty when the user clicks a product from widget

the code i use is this. to set the PendingIntentTemplete

ProductWigetProvider.class

Intent detailActivityIntent = new Intent(context, ProductDetailActivity.class);
    TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
    taskStackBuilder.addParentStack(ProductDetailActivity.class);
    taskStackBuilder.addNextIntent(detailActivityIntent);

    PendingIntent pendingIntent = taskStackBuilder.getPendingIntent( 1,PendingIntent.FLAG_UPDATE_CURRENT);

    views.setPendingIntentTemplate(R.id.widget_list_view,pendingIntent);

and this code to set the fillInIntent ProductRemoteViewFactory.class

Intent fillInIntent = new Intent();
    fillInIntent.setData(uri);
    views.setOnClickFillInIntent(R.id.widget_list_item_container,fillInIntent);

and my Manifest is

<activity
        android:name=".ui.EditProductActivity"
        android:label="@string/title_activity_add_new_item"
        android:parentActivityName=".ui.ProductDetailActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.dharmaraj.inventorymanager.ui.ProductDetailActivity" />
    </activity>

when i opene DetailActivty from the widge, and then i press back, the app closes without opening the main CatalogActivty.

1

There are 1 best solutions below

1
On BEST ANSWER

Try something similar to the following code(which is meant to launch a details activity from a notification):

Intent backIntent = new Intent(context, MainActivity.class);
        backIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Intent action = new Intent(context, DetailActivity.class);

PendingIntent operation =
                PendingIntent.getActivities(this, 0, new Intent[] {backIntent, action}, PendingIntent.FLAG_CANCEL_CURRENT);

Hope that helps!