1

I want to reorder an Activity A to front when notification is clicked and there will only have a single instance of Activity A.

for example:
Currently I have 3 activities launched by this order: Main->A->B

Now I clicked the notification and launch Activity A,
and I expect the order of stack should be: Main->B->A

However, what I've got was: Main->A->B->A

What would be the appropriate approach to do this stuff?

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClick(View view) {
        Common.onClick(this, view);
    }
}


public class ActivityA extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClick(View view) {
        Common.onClick(this, view);
    }
}


public class ActivityB extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClick(View view) {
        Common.onClick(this, view);
    }
}


public class Common {

    public static void onClick(Activity activity, View view) {
        Intent intent;
        switch (view.getId()) {
            //new activity anyway
            case R.id.btnA:
                intent = new Intent(activity, ActivityA.class);
                activity.startActivity(intent);
                break;
            case R.id.btnB:
                intent = new Intent(activity, ActivityB.class);
                activity.startActivity(intent);
                break;
            //reorder activity to front
            case R.id.btnAF:
                intent = new Intent(activity, ActivityA.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                activity.startActivity(intent);
                break;
            case R.id.btnBF:
                intent = new Intent(activity, ActivityB.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                activity.startActivity(intent);
                break;
            //new activity anyway through notification
            case R.id.btnNA:
                intent = new Intent(activity, ActivityA.class);
                sendNotification(activity, intent, "New A", "New A");
                break;
            case R.id.btnNB:
                intent = new Intent(activity, ActivityB.class);
                sendNotification(activity, intent, "New B", "New B");
                break;
            //reorder activity to front through notification
            case R.id.btnNAF:
                intent = new Intent(activity, ActivityA.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                sendNotification(activity, intent, "A to Front", "A to Front");
                break;
            case R.id.btnNBF:
                intent = new Intent(activity, ActivityB.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
                sendNotification(activity, intent, "B to Front", "B to Front");
                break;
        }
    }

    private static void sendNotification(Activity activity, Intent targetIntent, String title, String msg) {
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(activity)
                        .setSmallIcon(R.drawable.abc_ic_menu_copy_mtrl_am_alpha)
                        .setContentTitle(title)
                        .setContentText(msg);

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(activity);

        stackBuilder.addNextIntent(targetIntent);
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager mNotificationManager =
                (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE);

        mNotificationManager.notify(123456789, mBuilder.build());
    }
}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Activity" />

        <Button
            android:id="@+id/btnA"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="New A" />

        <Button
            android:id="@+id/btnB"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="New B" />

        <Button
            android:id="@+id/btnAF"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="A to Front" />

        <Button
            android:id="@+id/btnBF"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="B to Front" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Notification" />

        <Button
            android:id="@+id/btnNA"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="N New A" />

        <Button
            android:id="@+id/btnNB"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="N New B" />

        <Button
            android:id="@+id/btnNAF"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="N A to Front" />

        <Button
            android:id="@+id/btnNBF"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="N B to Front" />
    </LinearLayout>
</LinearLayout>
Season
  • 1,178
  • 2
  • 22
  • 42
  • http://stackoverflow.com/questions/20695522/puzzling-behavior-with-reorder-to-front – Francesc Mar 09 '16 at 16:33
  • @Francesc thesituation isn't same. That post is from A->B then Home->A – Season Mar 09 '16 at 16:36
  • @Francesc also that post is using startActivity, but what I want is trigger through notification – Season Mar 09 '16 at 16:38
  • Hi have you found out the solution? I just came across this issue. What I am doing is create a dummy activity with transparent background. So when the user clicks on the notification, it will bring out the activity, and all handling is done in that activity, of course, flag_activity_reorder_to_front will ok there. And this user won't see any differences. You just need to call `finish()` in `onCreate` in that dummy activity. – Arst Feb 13 '17 at 00:13

0 Answers0