The last time said, users choose whether to upgrade, if the user chooses not to upgrade, then the main interface to enter the program. The next thing to do is to jump from the splash interface to the main interface.
Mainactivity Create
1. First create a new mainactivity:
Package Com.liuhao.mobilesafe.ui;import Com.liuhao.mobilesafe.r;import Android.app.activity;import Android.os.bundle;public class Mainactivity extends Activity { @Override protected void OnCreate (Bundle Savedinstancestate) { super.oncreate (savedinstancestate); Set the activity content from a layout resource. The resource would be inflated, adding all top-level the activity Setcontentview (R.layout.main); }
2.MainActivity corresponding layout file: Main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:paddingbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_ Horizontal_margin " android:paddingright=" @dimen/activity_horizontal_margin " android:paddingtop=" @dimen /activity_vertical_margin " tools:context=". Splashactivity "> <textview android:layout_width=" wrap_content " android:layout_height=" Wrap_ Content
Configuring Mainactivity in 3.androidmanifest.xml
<activity android:name= "com.liuhao.mobilesafe.ui.MainActivity" android:label= "@string/main_screen" > </activity>
This completes the creation of the mainactivity, the following is the splashactivity to mainactivity switch.
Switch from splashactivity to mainactivity
New Loadmainui () method in Splashactivity
private void Loadmainui () { Intent Intent = new Intent (this, mainactivity.class); StartActivity (intent); Finish (); Remove the current activity from the task stack }
Then call this method where you need to go to the main interface before:
①
if (versiontext.equals (version)) { log.i (TAG, "version number is the same, no upgrade required, go to the main interface"); Loadmainui (); return false; }
Ii
catch (Exception e) { e.printstacktrace (); /** * Toast Usage Scenario * 1, need to prompt the user, but do not need the user to click the "OK" or "Cancel" button. * 2, does not affect the existing activity running simple hints. */ Toast.maketext (This, "Get update Information exception", 2). Show ()//Popup text and keep 2 seconds log.i (TAG, "Get update information exception, go to Main interface"); Loadmainui (); return false; }
③
Builder.setnegativebutton ("Cancel", new Onclicklistener () { @Override public void OnClick (Dialoginterface dialog , int which) { log.i (TAG, "User cancels the upgrade, enters the program main interface"); Loadmainui (); } });
Knowledge points
- Task stack taskstack and activity management
The code above is an example of starting another activity through intent in one activity. As mentioned earlier, one activity can start another, including those defined in different applications.
An activity is a user interface that can start a lot of activity continuously to improve the user experience. Of course, this quantity is limited, this depends on the phone's hardware configuration to decide. As already described in the previous article, all open activity is stored in the stack, so if you open too much activity, it takes up a lot of memory and processor resources, which can cause the current app to fail to start or crash the system.
Activity is closely related to the task. Because in the Android system, in order to maintain the user experience, the user wants to do something with the structure of the task as logic, in the form of application to express. And an application can contain a lot of activity, so it involves the relationship between activity and task.
- Activity Simple Explanation
Simply put, the task is the "application" that the user experiences. It is a set of related activity that is assigned to a stack. The root activity in the stack is the start of the task-in general, it is the activity selected in the user Group application loader. The activity at the top of the stack is what is currently running-the one that focuses on the user's actions. When one activity launches another, the new activity will be pushed into the stack, which will become a running activity. The previous activity remains in the stack. When the user presses the Back button, the current activity pops the stack, and the previous activity reverts to the running state.
onCreate ()------->onStart()-------->onresume()---------> Onsaveinstancestate()----->onPause()------->onStop ()---------> OnDestroy().
- Transitions of various states of activity:
When starting an activity, the first is the information needed to initialize the interface at OnCreate, such as: interface views,buttons, assigned reference variables, etc., when the initialization is done, the onstart is called, and the interface is now visible. The Onresume function is called when the user interacts with the interface, and when the activity enters the pause state because it is not fully covered by other activity, the OnStop function is called when it is completely overwritten by other activity. In both cases later, the Onsaveinstancestate method is called to temporarily save the state of the overwritten activity, which is restored when the overwritten activity is returned to the interface; When the finish method is called, the The activity is destroy and removed from the stack.
A mobile app typically contains multiple activities. Each activity is designed to accomplish some definite function and jump to the activity of other applications. For example, a mail messaging application with a title list activity, when clicked on the list title, jumps to another activity to display the contents of the message.
In one activity, you can also open an activity for another application on the same device. For example, when you send an e-mail message, you send a message with the intention of intent, which intent contains the email address, the mail message. Another send mail application defines how to receive this intent intent, so you can open the mail-sending application for email. If multiple applications support processing of this intent intent, then the system prompts you to select one of the executions. When you're done, you'll go back to your own application, which looks like a mail-sending application is part of your own application. Anyway, after all, it's the activities of different applications, and the task of the Android system makes the user perception completely dissimilar.
A task is a activities collector that collects activity that is opened by the interaction of the user operation. These activities are arranged in a recycle stack back stack, and the order of arrangement is consistent with the order in which they are opened. That is, the first open arrangement at the bottom, the last open arrangement at the top.
The main screen of the device is to place more tasks. When a user clicks an app icon to open an app, the task is at the front end. If the application has not been opened before, a new task will be created.
When the current activity opens another activities, the newly opened activity is at the top of the back stack and the user gets the focus state, and the current activity is saved in the stack and is in the stopped state. When the user taps the back key, the activity at the top of the stack is destroyed and the previous activity is restored. The back stack operation follows the principle of "last in, first out".
If the user continues to press RETURN, all activity in the stack will be ejected until the user returns to the home screen (or where the task begins). When all activity is removed from the stack, the task ceases to exist.
A task is a complete unit that can run in the background. When a user opens a task task, or jumps to the home screen via the home key, the task is running in the background, but all of the activities are in the stopped state. But the complete data for the back stack maintainer is simply the replacement of the focus by other tasks.
Figure 2. Two tasks: Task B to the front desk, task A then was into the backstage, waiting for recovery.
For example, there are three activity in the stack of the current task (Task a)-two of which are below the current activity. At this point, the user presses the home key back to the home screen and launches a new app. When the home screen is displayed, task a enters the background. When the new app starts, a new task (Task B) is launched for the app. Once the user has finished interacting with the app, go back to the main interface and select the app for task A. At this point, task a returns to the foreground-the three activity in the stack is intact and resumes activity at the top of the stack. At this point, the user can still return home to the home screen, select the app icon for task b to switch to Task B (also can be started by using the app list recently). This is an example of Android multitasking.
Note : Multiple tasks can exist in the background at the same time. However, if the user is running multiple background tasks, the system may destroy the background activity to reclaim memory, causing the state of the activity to be lost. This is explained in the following subsections.
Because activity in the fallback stack is never re-queued, if your app allows a user to start a specific activity from multiple activity, a new instance of the activity is created and placed on top of the stack. Therefore, an activity in your app may be instantiated several times, as shown in. Therefore, the user returns using the fallback key, and each instance of the activity is displayed in the reverse order in which it was opened. However, if you do not want to instantiate an activity multiple times, you can modify this behavior. about how to modify it, we'll discuss it later in the "Task Management" section.
Figure 3. An activity is instantiated several times.
Let's summarize the activity and the default behavior of the task:
- When activity a starts activity b,activity a stops, but the state of activity A is saved (for example, scroll bar position, text in the edit box, etc.). If the player presses the back key when activity B is active, it resumes activity A with the saved state.
- When the user presses the home key to leave a task, the current activity is stopped and the current task enters the background. The system saves the status of all activity in the task. If the user starts the task again through the start icon, the task returns to the foreground and resumes the activity at the top of the stack.
- If the user presses the next fallback key, the current activity is popped out of the stack and destroyed. The previous activity in the stack was restored. When an activity is destroyed, the system does not save the activity's state.
- The activity is instantiated several times, even if it is started by another task.
As described in the previous section, the system will save its state by default when the activity is stopped. As a result, when the user navigates to the previous activity, the user interface is displayed as if it were left. Then you can-and should-use your activity's callback method in advance to maintain its state, because the activity may be destroyed and recreated. When the system stops one of your activitie (such as when a new activity starts or the task is moved to the background), the system may completely destroy that activity in order to free up memory. When this happens, the status of the activity is lost. If this happens, the system still knows that activity occupies a position in the back stack, but when the activity gets to the foreground, the system must recreate it ( not just restore it ). To avoid losing the user's work, you should save the State in advance by implementing the activity's Onsaveinstancestate ().
- How to perform task management
The behavior that you just described is the default behavior of activity and task. The way Android manages tasks and back stacks, as described in the previous article, is done by putting all the activity that was started on the same task and in the same last-in-first-out stack. You don't need to worry about how your activites interact with task tasks, how they exist in the back stack. However, you may want to change this normal way of managing. For example, you want one of your activity to be managed in a new task, or you just want to instantiate an activity, or you want to clean up all the activities in a task when the user leaves the task, except for the root activity.
To accomplish these special management methods, activity and task associations, activity behavior in the task, controlled by the behavior of the initiating activity of the object's flag and the manifest file in the <activity> elements of the interaction between the attributes.
- In the manifest file, set some properties on the activity component that receives the request (that is, the intent object).
- Set some tokens in the sent request (that is, the intent object).
Therefore, both the requester and the receiver can control.
The properties that the activity can configure in manifest are:
- Taskaffinity
- Launchmode
- Allowtaskreparenting
- Cleartaskonlaunch
- Alwaysretaintaskstate
- Finishontasklaunch
Intents logos are:
- Flag_activity_new_task
- Flag_activity_clear_top
- Flag_activity_single_top
Note : Most apps should not change the default behavior of activity and tasks. If you are sure you have to change the default behavior, you must be careful and be sure to test the availability of activity at startup and back when navigating to it. Ensure that navigation behaviors that conflict with user habits are tested.
- Defining the startup mode
You can define how the activities interacts with the task by defining the run mode. The two methods defined are as follows:
1. Using the manifest file
When you declare an activity in your manifest file, you can specify how activity is associated with the task at startup .
2. Use the flag of intent
When you call StartActivity (), you can include in intent a flag that indicates how the new activity is (or is) associated with the current stack.
Example: Activity A initiates activity B. If b defines the run mode in manifest, and a is specified in intent for the run mode of B at startup B, the definition of a in intent overrides the definition of B in manifest.
Note: Some of the operating modes defined in manifest are not valid in intent, and similarly, the intent definition of a valid operating mode does not necessarily take effect in manifest.
- How to use manifest configuration management
In the manifest configuration file, you can specify different run modes in 4 through the Launchmode property of the <activity> element, specifying how the activity is associated with a task. The Launchmode property indicates how the activity is launched into a task. There are four different startup modes you can use to assign to the Launchmode property:
1. Standard: Default mode
In this default mode, activity can be instantiated more than once, or it can run in multiple tasks, and a task can have multiple instances of activity. Activity can be instantiated multiple times, and each instance can belong to a different task or to the same task.
2. Singletop
In this mode, if an activity instance already exists at the top of the current task, the system will call the Onnewintent () method to route to the instance instead of creating a new activity instance.
An activity can be instantiated multiple times and can be subordinate to a different task task, and there can be more than one activity instance in a task (this situation exists only if the activity instance is not at the top of the task).
Suppose that there is a root activitya-b-c-d,d at the top of the back stack of a task: then the situation in the D,back stack is opened:
In standard mode, an instance of a new class is started and the stack becomes a-b-c-d-d
In Singletop mode, the existing Activityd receives intent through onnewintent (), because it remains at the top of the stack-the stack continues to be a-b-c-d
If you turn on B
is a-b-c-d-b in singletop mode
Note : When an instance of a new activity is created, the user can press the back key to return to the previous activity. However, when an existing activity instance processes a new intent, the user cannot press back to return to the state of the current activity before intent.
3. Singtask
In this mode, the system creates a new task and instantiates the activities at the bottom of the task. However, when an activity instance exists in another independent task, the system is not going to create a new instance, but instead calls Onnewintent () to route to an instance of the other task. At the same time, there is only one instance of activity.
Note: Although the activity is started in a new task, the back key can still be returned to the previous activity.
4. Singinstance
Similar to Singtask, but only a little different is that this instance can only be used in a single task . Activity is always the only task, and any other activity initiated by the activity is opened in another task.
Next, for the back stack, let me give you an example:
Android Browser app claims web browsing activity must be open in its own task-by specifying the Singletask startup mode in the <activity> element. This means that if your app issues a intent to open the Android browser, its activity will not be placed in the same task as your app. Instead, a new task might start for the browser, or, if the browser is already running in the background, the task it is in is getting to the foreground and accepting the intent.
Regardless of whether a task starts activity from a new task or is started in an activity that already exists, the back key can always go back to the previous activity. However, if you start an activity in task A that is declared Singletask mode, the activity may already have an instance in the background that belongs to a task (Task B). Task B was then taken to the foreground and dealt with the new intent. The back key then returns to the task Bactivity, then back to the top activity of task A. Figure 4 illustrates this scenario.
Figure 4. Demonstrates how a "singletask" boot mode acitvity is added to a back stack. If the activity is already part of a background task (Task B) 's own stack, then the entire fallback stack is taken to the foreground, on top of the current task (Task a).
Note: The behavior of the specified actvitiy that you use the Launchmode property can be overridden by the intent flag.
- How to use intent configuration management
When you start the activity, you can implement a similar function to the manifest configuration by passing a intent into the startactivity () method to change the behavior of the activity in the task.
1.flag_activity_new_task
Starting activity-in a new task is not started in this task. If a task that contains this activity is already running, the task is taken to the foreground and the UI state is restored, and the existing activity receives the new intent in onnewintent ().
This symbol produces the same behavior as "Singletask".
2.flag_activity_single_top
If the activity being started is the current activity (at the top of the back stack), then the existing instance receives a call to Onnewintent () instead of creating a new instance.
This produces the same behavior as the "Singletop" mode.
3.flag_activity_clear_top
If the activity to be started is already running in the current task, then all other activity on top of it is destroyed, then the activity is restored, and through Onnewintent (), Initent is sent to this activity (now at the top of it)
There is no Launchmode attribute value corresponding to this behavior.
Flag_activity_clear_top is associated with flag_activity_New_task Most of the time. This can be achieved by finding the activity that exists in the other task and placing it in a place that can be intent accordingly. In standard mode, it is removed from the stack and a new activity is created to the corresponding intent, since this mode always creates a new activity.
Note: If the activity's startup mode is "standard", Flag_activity_clear_top will cause the existing activity to be removed from the stack and create a new instance in this location to handle the incoming intent. This is because the standard mode causes a new instance to always be created for the new intent.
- Differences in startup mode
- which task holds the activity and is used to respond to the behavior. for the "standard" and "singletop" modes, this task is the one that generates the behavior (and calls StartActivity ())-Unless the Behavior object contains a Flag_activity_new_task tag. In this case, as described in the previous section affinities and new tasks, a different task will be selected.
- whether they can have more than one instance. activity of type "standard" and "singletop" can be instantiated multiple times. They can belong to multiple tasks, and a specific task can have multiple instances of the same activity.
Activity that compares "Singletask" and "singleinstance" types is limited to one instance. Because these activity is the root of the task. This restriction means that you cannot have more than one instance of a task on the device at the same time.
- is there any other activity that can be found in the task it is in? an activity of type "singleinstance" is the only activity in the task in which it is located. If it launches other activity, regardless of the activity's startup mode, it will be loaded into a different task-like the flag_activity_new_task tag in the behavior object. In other respects, the "singleinstance" and "singletask" modes are the same.
There are more than one activity in the other three modes of running the task. "Singletask" is always the root activity in a task, but it can start other activity and assign it to the task in which it resides. The "standard" and "singletop" types of activity can be found anywhere in the task.
- whether to start a new instance to handle a new behavior. for the default "standard" mode, a new instance will be created for each behavior in response. Each instance handles only one behavior. For the "singletop" mode, if an existing instance is located at the top of the target task activity stack, then he will be reused to handle the behavior. If it is not on top of the stack, it will not be reused, but instead create a new instance of the behavior and press it into the stack.
- Handling Task Affinity Relationships
By default, activity components in an application are related to each other, meaning that they belong to the same task stack. But we can set a kinship for this activity component by setting the taskaffinity property of a <activity> tag. Activity components defined in different applications can share the same kinship, or the activity component defined in the same application can use a different kinship.
Kinship will work in two situations:
1) The intent object that is responsible for activating the ACTIVITY component contains the flag_activity_new_task flag.
By default, when you create a new activity, the StartActivity () method is called to enter the task. It is placed into the same back stack as the activity that initiates it. However, if the startup intent contains the Flag_activity_new_task identity, the system will look for a different task for the ACTIVITY. Typically, you create a new task. This is not necessarily the case, if there is a task with the same taskaffinity definition, then the activity will run there, otherwise create a new task.
2) The allowtaskreparenting property of the active activity component is set to "true".
If the allowtaskreparenting of an activity component is set to "true", then when the task stack with the same kinship as the activity is switched to the foreground, The activity moves from the existing task stack to the task stack with the same kinship.
Give an example of the effect of this property:
Suppose an activity that chooses a city to see the weather is part of a travel app. This activity has the same affinity as other activity in the app, and this property is set to true. When your other activity initiates this activity to view the weather, it is the same task as your activity. However, when your travel app is displayed again on the front end, this activity that looks at the weather is reassigned to the travel application's task and displays the weather conditions.
If, from the user's point of view, an. apk file contains more than one "application", you may want to specify a different kinship for those activity components.
- How to clean up the back stack
If a user leaves a task for a long time, the system cleans up the task, except, of course, the root activity. When the user comes back, only the root activity is left, and the rest is destroyed. The system does this because, after a long period of time, the user is likely to abandon what they have already done and go back to the task and start doing something new.
The following properties can be used to set and change this behavior:
If this property is set to true in the activity of the task, the default behavior will no longer occur and all activity's state data will be persisted.
If you set this property to true in the activity of a task, when the user leaves or returns to the task, the activity will be cleaned up except for the root activity. In other words, it comes in the opposite way with Alwaysretaintaskstate. When the user returns to the task, they always see the initial state, even if they leave for a short while.
Similar to Cleartaskonlaunch, but only in a single activity , not the entire task, it can clean up any activity, including the activity. You save activity for the current session, and when the user returns or leaves, it will not be restored.
The category is set to "Android.intent.category.LAUNCHER" by setting the action of Intent-filter for an activity type to "Android.intent.action.MAIN" This activity instance can be referred to as a portal to a task stack. Charts and names of activity types that have this type of Intent-filter are also displayed in the Application Launcher.
For example:
<activity > <intent-filter ... > <actionandroid:name= "Android.intent.action.MAIN"/> <categoryandroid:name= "Android.intent.category.LAUNCHER"/> </intent-filter></activity >
A intent type of filter causes an icon and tag of activity to be displayed on the App Launcher screen, allowing the user to initiate the activity and return to the task again. The second ability is important: the user must be able to switch a task stack to the background or switch it to the foreground at any time. For this reason, the start-up mode (i.e. "Singletask" and "singleinstance" mode) that enables activity to launch the newly opened task stack should only be exploited in owning the "Android.intent.action.MAIN" Action and " Android.intent.category.LAUNCHER "category of Intent-filter activity type.
Imagine what happens if you don't have these two filters: a behavior that initiates the activity of the "Singletask" mode, initiates a new task, and the user spends some time on the task. Then the user presses the home button, and the task is hidden behind the scenes. Because it is not displayed on the application loader, there is no way to return to this task.
A similar trouble thing flag_activity_new_task sign. If this flag causes the activity to start a new task, and the user presses the home key to leave it, there must be some way to direct the user back to it. Some entities, such as notification managers, always initiate activity in an external task, not as part of them, so they always pass the behavior object with the Flag_activity_new_task tag to StartActivity (). If you have an activity that can be invoked by an external entity using this tag, be aware that the user should have a way to return to the startup task.
But what happens when you don't want users to be able to go back to an activity? There's a way: Set the <activity> element's Finishontasklaunch property to "true"!
Reference:
http://philn.blog.163.com/blog/static/10401475320135144639141/
http://blog.csdn.net/wbw1985/article/details/4916909
http://blog.csdn.net/oracleot/article/details/19036909
http://blog.163.com/cazwxy_12/blog/static/898763720122992149143/
"Side doing project learning Android" Mobile security defender 04_01: Switch between interface (activity), Activity and task stack