Activityのプロパティ-launchMode


Activityのプロパティ-launchMode
manifestインベントリファイルでactivityを構成する場合、launchModeという属性があります.
 android:launchMode=["multiple" | "singleTop"
                             
"singleTask" | "singleInstance"

/>
LaunchModeはこのactivityのロードモードを指定する、上述のようにロードモードは4種類あり、それぞれstandard singleTopsingleTaskとsingleInstanceである.
なぜactivityのロードモードを指定するのですか?Androidシステムはactivityをスタックで管理するため、アプリケーションが起動すると対応するシステムがこのアプリケーションのスタックを作成し、アプリケーションのmain activityを起動します.
本アプリケーションのactivityは、データ構造におけるアウトスタックとインスタックと同様に管理される.activityのロードモードではactivityのインスタンス化やロード方式、ロードの順序関係などを管理できます.
では、この4つのロードモードについて説明します.
1、 standard
このモードはactivityのデフォルトの起動モードです.つまり、activityのlaunchModeを指定しない場合にこのモードで起動します.このモードでは、新しいactivityを起動するたびにターゲットactivityに新しいインスタンスが作成されます.このインスタンスをスタックに追加します(つまり、スタックの上部にあります).PSの一般的なお金は、ユーザーに表示されるactivityがスタックの上部にあります.
 
2、 singleTop
このモードでは、ターゲットのactivityを起動するときに、現在適用されているスタックのスタックトップがターゲットのactivityのインスタンスであれば、新しいインスタンスは再作成されず、スタックトップのインスタンスを直接使用します.(起動ターゲットがスタックトップにない場合、activityのインスタンスが再作成され、スタックトップにロードされます.standardモードとまったく同じです).
3、 singleTask
このモードでは、現在のスタックにactivityを起動するインスタンスがある場合、このインスタンスを直接呼び出します.つまり、同じスタックに1つのインスタンスしかありません.singleTopモードを使用すると、スタックトップアクティビティの重複作成の問題をうまく解決できますが、activityがスタックトップの位置にない場合は、複数のactivityインスタンスが作成される可能性があります.では、あるactivityをアプリケーション全体のコンテキストに1つのインスタンスしか存在させる方法はありませんか?これはsingleTaskモードによって実現される.activityの起動モードがsingleTaskとして指定されている場合、activityを起動するたびに、システムはまず戻りスタックでactivityのインスタンスが存在するかどうかをチェックし、存在が発見されたら直接そのインスタンスを使用し、このactivityの上にあるすべてのactivityをスタックから取り出し、発見されなければ新しいactivityインスタンスを作成します.
4、 singleInstance
singleInstanceモードは4つの起動モードの中で最も特殊で複雑な1つであるべきで、あなたもこのモードを理解するのにもっと工夫する必要があります.以上の3つの起動モードとは異なり、singleInstanceモードに指定されたactivityは、このactivityを管理するために新しい戻りスタックを有効にします(singleTaskモードが異なるtaskAffinityを指定している場合、新しい戻りスタックも起動します).では、このようにするのは何の意味がありますか?次のシーンを想像して、私たちのプログラムの中にactivityが他のプログラムの呼び出しを許可していると仮定します.もし私たちが他のプログラムと私たちのプログラムがこのactivityのインスタンスを共有できることを実現したいなら、どのように実現すればいいですか?前の3つの起動モードを使用すると、アプリケーションごとに独自の戻りスタックがあるため、同じactivityが異なる戻りスタックにスタックを入れるときに必ず新しいインスタンスが作成されます.singleInstanceモードを使用すると、この問題を解決できます.このモードでは、このactivityを管理するために個別の戻りスタックがあり、どのアプリケーションがこのactivityにアクセスしても、同じ戻りスタックを共有し、activityインスタンスを共有する問題を解決します.
 
ちなみにapiドキュメントの紹介クリップを見てみましょう.
より抜くhttp://developer.android.com/guide/topics/manifest/activity-element.html#lmode
  • android:launchMode
  • An instruction on how the activity should be launched. There are four modes that work in conjunction with activity flags ( FLAG_ACTIVITY_* constants) in Intent objects to determine what should happen when the activity is called upon to handle an intent. They are:"standard ""singleTop ""singleTask ""singleInstance "The default mode is "standard ".As shown in the table below, the modes fall into two main groups, with "standard "and "singleTop "activities on one side, and "singleTask "and "singleInstance "activities on the other. An activity with the "standard "or "singleTop "launch mode can be instantiated multiple times. The instances can belong to any task and can be located anywhere in the activity stack. Typically, they're launched into the task that called startActivity() (unless the Intent object contains a FLAG_ACTIVITY_NEW_TASK instruction, in which case a different task is chosen — see the taskAffinity attribute).In contrast, "singleTask "and "singleInstance "activities can only begin a task. They are always at the root of the activity stack. Moreover, the device can hold only one instance of the activity at a time — only one such task.The "standard "and "singleTop "modes differ from each other in just one respect: Every time there's a new intent for a "standard "activity, a new instance of the class is created to respond to that intent. Each instance handles a single intent. Similarly, a new instance of a "singleTop "activity may also be created to handle a new intent. However, if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent() call); a new instance is not created. In other circumstances — for example, if an existing instance of the "singleTop "activity is in the target task, but not at the top of the stack, or if it's at the top of a stack, but not in the target task — a new instance would be created and pushed on the stack.Similarly, if you navigate up to an activity on the current stack, the behavior is determined by the parent activity's launch mode. If the parent activity has launch mode singleTop (or the up intent contains FLAG_ACTIVITY_CLEAR_TOP ), the parent is brought to the top of the stack, and its state is preserved. The navigation intent is received by the parent activity's onNewIntent() method. If the parent activity has launch mode standard (and the up intent does not contain FLAG_ACTIVITY_CLEAR_TOP ), the current activity and its parent are both popped off the stack, and a new instance of the parent activity is created to receive the navigation intent.The "singleTask "and "singleInstance "modes also differ from each other in only one respect: A "singleTask "activity allows other activities to be part of its task. It's always at the root of its task, but other activities (necessarily "standard "and "singleTop "activities) can be launched into that task. A "singleInstance "activity, on the other hand, permits no other activities to be part of its task. It's the only activity in the task. If it starts another activity, that activity is assigned to a different task — as if FLAG_ACTIVITY_NEW_TASK was in the intent.
    Use Cases
    Launch Mode
    Multiple Instances?
    Comments
    Normal launches for most activities
    "standard "
    Yes
    Default. The system always creates a new instance of the activity in the target task and routes the intent to it.
    "singleTop "
    Conditionally
    If an instance of the activity already exists at the top of the target task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity.
    Specialized launches(not recommended for general use)
    "singleTask "
    No
    The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.
    "singleInstance "
    No
    Same as "singleTask" , except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task.
    As shown in the table above, standard is the default mode and is appropriate for most types of activities. SingleTop is also a common and useful launch mode for many types of activities. The other modes — singleTask and singleInstance — are not appropriate for most applications, since they result in an interaction model that is likely to be unfamiliar to users and is very different from most other applications.Regardless of the launch mode that you choose, make sure to test the usability of the activity during launch and when navigating back to it from other activities and tasks using the Back button.For more information on launch modes and their interaction with Intent flags, see the Tasks and Back Stack document

  • の の の は くなくて、 を します[email protected]