IT박스

안드로이드 : Clear Activity Stack

itboxs 2020. 7. 13. 21:41
반응형

안드로이드 : Clear Activity Stack


신청서에 몇 가지 활동이 있습니다. 흐름이 매우 복잡합니다. Logout 응용 프로그램을 클릭하면 로그인 화면으로 이동하고 거기에서 사용자가 취소 버튼을 눌러 종료 할 수 있습니다 (호출 system.exit(0))

나가거나 뒤로 버튼을 누르면 시스템이 스택에서 활동을 호출합니다. (로그인 화면에 도달하면 스택의 모든 활동을 어떻게 지울 수 있습니까?) 호출 finish()이 실용적이지 않으므로 호출 이 실용적이지 않으며 일부 활동이 종료되면 안됩니다 기본 카메라 호출 활동과 같은 활성입니다.

validateuser logoutuser = new validateuser();
logoutuser.logOut();
Intent loginscreen = new Intent(homepage.this, Login2.class);
(homepage.this).finish();
loginscreen.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(loginscreen);

대부분의 사람들이 틀 렸습니다. 내용에 관계없이 기존 활동 스택을 닫고 새 루트를 작성하려면 올바른 플래그 세트는 다음과 같습니다.

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

문서에서 :

public static final int FLAG_ACTIVITY_CLEAR_TASK
API 레벨 11에 추가됨

에 전달 된 인 텐트로 설정된 Context.startActivity()경우이 플래그는 활동이 시작되기 전에 활동과 연관된 기존 태스크가 지워지게합니다. 즉, 활동이 빈 작업의 새 루트가되고 이전 활동이 완료됩니다. 이것은와 함께 만 사용할 수 있습니다 FLAG_ACTIVITY_NEW_TASK.


당신이 호출 할 때 startActivity마지막 활동에 항상 사용할 수 있습니다

Intent.FLAG_ACTIVITY_CLEAR_TOP

그 의도에 대한 플래그로.

국기에 대한 자세한 내용은 여기를 참조하십시오 .


다음은 API 레벨 4에서 현재 버전 17까지 작동하는 새로운 최상위 활동으로 새 활동을 시작하는 간단한 헬퍼 방법입니다.

static void startNewMainActivity(Activity currentActivity, Class<? extends Activity> newTopActivityClass) {
    Intent intent = new Intent(currentActivity, newTopActivityClass);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        intent.addFlags(0x8000); // equal to Intent.FLAG_ACTIVITY_CLEAR_TASK which is only available from API level 11
    currentActivity.startActivity(intent);
}

현재 활동에서 다음과 같이 호출하십시오.

startNewMainActivity(this, MainActivity.class);

아래 코드로 Activity Backstate를 지우 십시오.

Intent intent = new Intent(Your_Current_Activity.this, Your_Destination_Activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

끝난


필자의 경우 LoginActivity도 닫혔습니다. 결과적으로

Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK

도와주지 않았다.

그러나 설정

Intent.FLAG_ACTIVITY_NEW_TASK

helped me.


Intent intent = new Intent(LoginActivity.this, Home.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  //It is use to finish current activity
startActivity(intent);
this.finish();

This decision works fine:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

But new activity launch long and you see white screen some time. If this is critical then use this workaround:

public class BaseActivity extends AppCompatActivity {

    private static final String ACTION_FINISH = "action_finish";

    private BroadcastReceiver finisBroadcastReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        registerReceiver(finisBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                finish();
            }
        }, new IntentFilter(ACTION_FINISH));
    }

    public void clearBackStack() {
        sendBroadcast(new Intent(ACTION_FINISH));
    }

    @Override
    protected void onDestroy() {
        unregisterReceiver(finisBroadcastReceiver);
        super.onDestroy();
    }
}

How use it:

public class ActivityA extends BaseActivity {

    // Click any button
    public void startActivityB() {
        startActivity(new Intent(this, ActivityB.class));
        clearBackStack();
    }
}

Disadvantage: all activities that must be closed on the stack must extends BaseActivity


I noted that you asked for a solution that does not rely on finish(), but I wonder if this may help nonetheless.

I tracked whether an exit flag is raised with a static class variable, which survives the entire app lifespan. In each relevant activity's onResume(), use

@Override
public void onResume() {
    super.onResume();
    if (ExitHelper.isExitFlagRaised) {
        this.finish();
    }
}

The ExitHelper class

public class ExitHelper {
    public static boolean isExitFlagRaised = false;
}

Let's say in mainActivity, a user presses a button to exit - you can set ExitHelper.isExitFlagRaised = true; and then finish(). Thereafter, other relevant activities that are resumed automatically will be finished as well.


For Xamarin Developers, you can use:

intent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ClearTask);

참고URL : https://stackoverflow.com/questions/7075349/android-clear-activity-stack

반응형