ProgramingTip

New Intent ()는 Android에서 새 인스턴스를 시작합니다. launchMode =“singleTop”

bestdevel 2020. 12. 15. 19:40
반응형

New Intent ()는 Android에서 새 인스턴스를 시작합니다. launchMode =“singleTop”


매니페스트에 활동 Aandroid:launchMode="singleTop"있습니다.

나는 활동에가는 경우에 B, C그리고 D내 응용 프로그램 루트 활동으로 돌아가려면 메뉴 바로 가기가 있습니다 ( A).

코드는 다음과 가변적입니다.

Intent myIntent = new Intent(getBaseContext(), MainActivity.class);
startActivity(myIntent);

그러나 이미 존재 A하는 내 인스턴스로 돌아가는 대신 MainActivity.class새 인스턴스를 만듭니다-> onCreate()대신 onNewIntent().

이것은 예상되는 동작이 아닙니다.


이것은 트릭을 할 것입니다.

<activity ... android:launchMode="singleTop" />

앱을 시작하려는 인 텐트를 만들 때 다음을 사용하세요.

Intent intent= new Intent(context, YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

이것이 필요합니다.


결국 나를 위해 효과가 떨어져 나가는 것입니다.

Intent myIntent = new Intent(getBaseContext(), MainActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);

문서 에서 인용 :

"standard"및 "singleTop"모드는 한 가지의 서로간에 서로입니다. "표준"활동에 대한 새로운 인 텐트가있을 때마다 해당 인 텐트에 응답하기 위해 클래스의 새 인스턴스가 생성됩니다. 각 인스턴스는 단일 인 텐트를 처리합니다. 많은 새 인 텐트를 처리하기 위해 "singleTop"활동의 새 인스턴스를 만들 수도 있습니다. 그러나 대상 작업의 스택 맨 위에 활동의 기존 인스턴스가 이미있는 경우 해당 인스턴스는 onNewIntent () 호출에서 새 인 텐트를 수신합니다. 새 인스턴스가 생성되지 않습니다.

"이미 스택 맨 위에 활동의 기존 인스턴스가 있음"이 무엇을 의미하는지 100 % 확실하지 않습니다.

겠습니까 singleTask또는 singleInstance당신을 위해 사용할 수 있습니까? 또는 FLAG_ACTIVITY_SINGLE_TOP당신이 만들려는 의도를 설정 하여 차이를 만드는 것을 시도 할 수 있습니다.


다음을 사용하여 기존 기존 활동 인스턴스로 돌아갈 수 있습니다. android:launchMode="singleInstance"

매니페스트에서. A에서 돌아올 때 B, finish()파기 해야하는 경우가 있습니다 B.


첫째, 스택 구조를 검토 할 수 있습니다. -mode의 시작
경우 : singleTop 동일한 활동의 인스턴스가 이미 작업 스택 의 맨 위에있는 경우이 인스턴스는 인 텐트에 응답하는 데 재사용됩니다 .

모든 활동은 스택 ( "선입 선출")에 보관하고 현재 활동이 스택의 맨 위에있는 manifest.file에서 singleTop으로 정의한 경우

android:name=".ActivityA"
android:launchMode="singleTop"

ActivityA에서 활동을 다시 작성하면 onCreate가 onNewIntent ()를 다시 시작하고 Not : İf를 구현하지 새 의도를 얻지 못함을 알 수 있습니다.

Intent activityMain = new Intent(ActivityA.this,
                        ActivityA.class);

                activityMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_SINGLE_TOP);
                startActivity(activityMain);




    @Override
        protected void onNewIntent(Intent intent) {

            super.onNewIntent(intent);

            notify("onNewIntent");
        }

        private void notify(String methodName) {

            String name = this.getClass().getName();
            String[] strings = name.split("\\.");

            Notification noti = new Notification.Builder(this)
                    .setContentTitle(methodName + "" + strings[strings.length - 1])
                    .setAutoCancel(true).setSmallIcon(R.drawable.ic_launcher)
                    .setContentText(name).build();
            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.notify((int) System.currentTimeMillis(), noti);

        }

이것은 원래 A 활동이 B, C 또는 D에서 이미 파괴되기 때문입니다. 따라서 onNewIntent () 대신 onCreate가 호출됩니다. 이 문제를 해결하는 한 가지 방법은 새 A를 시작하기 전에 항상 기존 기존 A (startActivity시 FLAG_ACTIVITY_NEW_TASK 연결)를 삭제하는 것 onCreate가 항상 호출되고 getIntent ()를 확인하여 onCreate에 onNewIntent ()를 설치합니다. )는 의도입니다.

참조 URL : https://stackoverflow.com/questions/2424488/new-intent-starts-new-instance-with-android-launchmode-singletop

반응형