launch activity twice even with launchMode="singleTask"

2020-02-26 java android android-intent android-activity launchmode

I have an activity for showing tour details with two inconsistent goals

First that activity has a booking button which redirects to bank pay and get back to activity after successful or unsuccessful pay.that is why i set Launch Mode in manifest to stop activity from re creating.

android:launchMode="singleTask"

Second that activity has a button which redirect to similar tour,then i have to call finish(); before startActivity() to make intent work!

onNewIntent() inside activity to get data of first part

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (intent.getData() != null) {
        Helper.logDebug("fsfsfsgsgsgs", "inside get data not null");
        String query = intent.getData().getQuery();
        Helper.logDebug("fsfsfsgsgsgs","query is "+ query);
        if (query!=null && query.contains("Status=OK")) {
            if (frgBtmReserve!=null){
                frgBtmReserve.dismiss();
            }
            String count=query.substring(16);
            Helper.logDebug("fsfsfsgsgsgs","count is "+ count);
            frgObjectInfo.updateReserveCount(count);
            Helper.logDebug("fsfsfsgsgsgs", "inside status ok");
            Helper.notifyUserDone(getResources().getString(R.string.success_tour_reserve), this,R.drawable.ic_tick);
        } else {
            frgBtmReserve.dismiss();
            Helper.logDebug("fsfsfsgsgsgs", "inside status nok");
            Helper.notifyUserWarning(getResources().getString(R.string.error_tour_reserve), this);
        }
    }
}

on click of second part which should create new instance of current activity but it doesnt because of launch mode of activity which is singleTask.intent doesnt work until i finish() before startActivity()

Intent intent = new Intent(context, ActivityShowObject.class);
            intent.putExtra(ActivityShowObject.INTENT_KEY_TYPE, Obj.TYPE_TRAVEL);
            intent.putExtra(ActivityShowObject.INTENT_KEY_COLOR, color);
            intent.putExtra(ActivityShowObject.INTENT_KEY_OBJ_ID, obj.getAgencyId());
            ((AppCompatActivity)context).finish();
            startActivity(intent);
            Animatoo.animateShrink(context);

that is my problem think of a user which is looking into some similar tours then press back and app goes back to the very first step! do you guys have a suggestion for me?

Answers

Get rid of

android:launchMode="singleTask"

for a start. I think its the wrong usecase, making it harder for you.

You have two scenarios:

  • 1) New activity pay for this tour
  • 2) New activity Browse another tour

for #2 use the normal startActivity route, this will allow normal back navigation

for #1 you could also use normal startActivity but I have a feeling that when they pay successfully you do not want them to press back to go back to the tour, but if they are unsuccessful at paying you do want to allow them to go back?

If that is the case, you can use startActivityForResult when navigating to your #1 pay scenario.

When they complete payment successfully call

setResult(RESULT_OK)

if they are unsuccessful

setResult(RESULT_CANCELLED)

Then when they hit back, the result is propogated to the first activity, and you an use this to either call finish() or not.

Related