Question

I have a bug in my application I can't figure out. There are three activities: HomeActivity - A, TestActivity - B and ResultActivity - C.

Activity A launches B in a normal way

Intent intent = new Intent(this, TestActivity.class);
startActivity(intent);

Then activity B launches C in the same way. When C is done it goes back to A clearing the stack like this

Intent intent = new Intent(this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

Now the process can be repeated ad infinitum, without any issues. However, I have a shortcut that lets the user start B from C via A by sending a parameter to A intent.putExtra("startTest", true); which A uses to start B. Now what happens is if I press the back button, being in activity B, the activity gets restarted in stead of going back to A.

The activity stack seems to be just fine according to adb shell dumpsys activity:

Activity stack:
* TaskRecord{408276f0 #60 A com.company.app}
clearOnBackground=false numActivities=3 rootWasReset=true
affinity=com.company.app
intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.company.app/.LoginActivity bnds=[120,241][240,391]}
realActivity=com.company.app/.LoginActivity
lastActiveTime=756688148 (inactive for 6s)
* Hist #12: HistoryRecord{40ab2248 com.company.app/.TestActivity}
    packageName=com.company.app processName=com.company.app
    launchedFromUid=10109 app=ProcessRecord{4089aed0 16527:com.company.app/10109}
    Intent { cmp=com.company.app/.TestActivity }
    frontOfTask=false task=TaskRecord{408276f0 #60 A com.company.app}
    taskAffinity=com.company.app
    realActivity=com.company.app/.TestActivity
    base=/data/app/com.company.app-1.apk/data/app/com.company.app-1.apk data=/data/data/com.company.app
    labelRes=0x0 icon=0x7f02000b theme=0x103000d
    stateNotNeeded=false componentSpecified=true isHomeActivity=false
    configuration={ scale=1.0 imsi=242/1 loc=nb_NO touch=3 keys=1/1/2 nav=1/1 orien=1 layout=34 uiMode=17 seq=22}
    launchFailed=false haveState=false icicle=null
    state=RESUMED stopped=false delayedResume=false finishing=false
    keysPaused=false inHistory=true launchMode=0
    fullscreen=true visible=true frozenBeforeDestroy=false thumbnailNeeded=false idle=true
    waitingVisible=false nowVisible=true
* Hist #11: HistoryRecord{40a548e8 com.company.app/.HomeActivity}
    packageName=com.company.app processName=com.company.app
    launchedFromUid=10109 app=ProcessRecord{4089aed0 16527:com.company.app/10109}
    Intent { flg=0x4000000 cmp=com.company.app/.HomeActivity }
    frontOfTask=false task=TaskRecord{408276f0 #60 A com.company.app}
    taskAffinity=com.company.app
    realActivity=com.company.app/.HomeActivity
    base=/data/app/com.company.app-1.apk/data/app/com.company.app-1.apk data=/data/data/com.company.app
    labelRes=0x0 icon=0x7f02000b theme=0x103000d
    stateNotNeeded=false componentSpecified=true isHomeActivity=false
    configuration={ scale=1.0 imsi=242/1 loc=nb_NO touch=3 keys=1/1/2 nav=1/1 orien=1 layout=34 uiMode=17 seq=22}
    launchFailed=false haveState=true icicle=Bundle[mParcelledData.dataSize=1192]
    state=STOPPED stopped=true delayedResume=false finishing=false
    keysPaused=false inHistory=true launchMode=0
    fullscreen=true visible=false frozenBeforeDestroy=false thumbnailNeeded=false idle=true

It doesn't matter how many times I press the back button. The stack remains the same, and activity B keeps on restarting.

Now to the even stranger part: If i double tap the back button, it does indeed traverse back into the stack giving me activity A.

I don't have any special handeling of the back button, and since it works well i case 1 but not case 2 I'm really lost. Searching hasen't really got me anything, so any insight would be appreciated.

EDIT Here are what I think are the relevant bits from the activities

Was it helpful?

Solution

As you made normal scenario Complex. If You have use startActivityForResult() and onActivityResult() with necessary Flags and Conditions in your Activities then Above problems never occurs..

Reason:

As I have doubt when you press Back Button from B Activity its call repeatedly protected void onBackendStarted() of HomeActivity And your Activity B keep Restarting..

Update:

You have to remove startTest from Intent Once Activity B is started. So next Time it will not Launch Repeatedly.

Something like:

getIntent().removeExtra("startTest"); to onBackendStarted().

OTHER TIPS

Try like this..

@Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        System.gc();
        Intent I = new Intent(EditProfile.this, SettingActivity.class);
        startActivity(I);
        finish();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top