Puja Surya
Puja Surya

Reputation: 1297

Error in FLAG_ACTIVITY_CLEAR_TOP

i have a 5 activity

1 >> 2 >> 3 >> 4 >> 5

the number 1 activity is first launch class.

number 2 is my list data class

number 3 and 4 is my input data class

number 5 is finishing class

in activity number 5 i put code in button to come back to previous when finished

i have two button back to previous activity.

  1. back to activity number 2
  2. back to activity number 1

here is button on number 5 activity

1).

Intent intent = new Intent(getApplicationContext(), 2.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
                startActivity(intent);

2).

Intent dashboard = new Intent(getApplicationContext(), 1.class);
                dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(dashboard);

EDIT

a. when i choose button number 2). in first build application, i click and back to number 1 activity. when I tap the back button , the activity not show number 5 activity again and it works.

senario a : 1 >> 2>> 3 >> 4 >> 5 >> 1 >> exit app

b. when i choose button number 2). in first build application, i try to input data again and at last in number 5 activity, i choose button number 1). i click and back to number 2 activity . when I tap the back button , the activity not shows number 5 activity again and it works.

senario b : 1 >> 2 >> 3 >> 4 >> 5 >> 1 >> 2 >> 3 >> 4 >> 5 >> 2 >> tap back button >> 1 >> tap back button >> exit app

c. but when i choose button number 1). in first build application. i click and back to number 2 activity. when I tap the back button , it shows Previous activity again or activity number 5.

senario c : 1 >> 2 >> 3 >> 4 >> 5 >> 2 >> tap back button >> 5

d. i want my button number 1). not show previous or activity number 5 when i tap the back button in my first build application or the last activity is not show without i choose button number 2). first.

scenario d : 1 >> 2 >> 3 >> 4 >> 5 >> 2 >> tap back button >> 1 >> exit app

how to fix that?

problem since yesterday till now

sorry for my bad english.

EDIT

in my manifest i declare

number 1 activity is MainMenu.class .

<activity
            android:name="hariff.ltis.mainmenu.MainMenu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

number 2 is MainPetakTetap.class

<activity
            android:name="hariff.ltis.petaktetap.MainPetakTetap"
            android:label="@string/title_activity_main_petak_tetap" >
        </activity>

activity number 3, 4, 5

<activity
            android:name="hariff.ltis.petaktetap.MainUpdate"
            android:label="@string/title_activity_main_petak_tetap" >
        </activity>

<activity
            android:name="hariff.ltis.petaktetap.UpdateLog"
            android:label="@string/title_activity_main_petak_tetap" >
        </activity>

<activity
            android:name="hariff.ltis.petaktetap.CPetakTempView"
            android:label="@string/title_activity_main_petak_tetap" >
        </activity>

Upvotes: 0

Views: 430

Answers (2)

Hoan Nguyen
Hoan Nguyen

Reputation: 18151

It seems to me that the system kill activity 2 and thus 2 is not in the stack anymore, thus it is now created anew and on top of 5. Thus when you back press you will get 5. A work around (I am not sure it will work) is as follow

  1. When starting activity 4 from activity 3 and activity 5 from 4 use startActivityForResult.

  2. In activity 4

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
        super.onActivityResult(requestCode, resultCode, intent);
    
        if (resultCode == RESULT_OK)
        {
             setResult(RESULT_OK);
             finish();
        }
    }  
    
  3. In Activity 3

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent)
    {
        super.onActivityResult(requestCode, resultCode, intent);
    
        if (resultCode == RESULT_OK)
        {
             new Intent(getApplicationContext(), 2.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
            startActivity(intent);
             finish();
        }
    } 
    
  4. In activity 5

    setResult(RESULT_OK);
    finish();
    

Upvotes: 1

Monty
Monty

Reputation: 3215

if you are going back from any activity to last activity.

please use finish() instead of creating new Intent

IN your code ,you are creating new Intent for activity 1.class,which is already exist .this is wrong

Intent dashboard = new Intent(getApplicationContext(), 1.class);
                dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(dashboard);

do this on button click

finish(); //this will finish your current activity which is in stack.

and try to use this or current activity name instead ofgetApplicationContext()`.

update : if you want to go any activity just use StartActivityForResult()

Upvotes: 0

Related Questions