A first glimpse into Activity


Introduction to this section:

In the previous section we learned about some basic concepts of Activity, what is Activity, the life cycle of Activity, how to start an Activity, etc. In this section we Let’s continue to learn about Activity. As mentioned before, an App is generally composed of multiple Activities. This involves the issue of data transfer between multiple Activities. So in this section, continue to learn about the use of Activity! In addition, we will explain about passing collections, objects, arrays, and Bitmaps in Intent. Here we only introduce how to pass basic data!


Data transfer between Activities:

1.png

## Code example:

Rendering:

2.gif

## Code download:

ActivityTest1.zip

2 .Interaction between multiple Activities (the latter one is passed back to the previous one)

3.png

Code example:

Effect Picture:

4.gif

# Code download:

ActivityTest2.zip

3. Know which Activity is currently


5.png4. Close all activities at any time


6.pngSometimes we may have many activities open, and suddenly there is such a demand, which can be turned off on a certain page. All activities and exit the program! Well, here is a method to close all activities, Just use a list collection to store all activities!

The specific code is as follows:

public class ActivityCollector {
public static LinkedList<Activity> activities = new LinkedList<Activity>();
public static void addActivity(Activity activity)
{
activities.add (activity);
}
public static void removeActivity(Activity activity)
{ activities.remove(activity);
}
public static void finishAll()
{
                                                                                                                                                                                                                       
}
}



5. Method to completely exit the App

The above mentioned is to close all activities, but sometimes we may want to kill the entire App. Even background tasks are killed If you want to kill them all, you can use it with the following code:
Implementation code:

/**

* Exit the application

*/ public void AppExit(Context context) { try {

ActivityCollector.finishAll();
ActivityManager activityMgr = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SER VICE);
activityMgr.killBackgroundProcesses (context.getPackageName());
System.exit(0);
} catch (Exception ignored) {} ​​
}


6. Two ways to exit the program by double-clicking:

1) Define a variable to identify whether to exit

// Define a variable to identify whether to exit Exit
private static boolean isExit = false;
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
isExit = false;
}
};

public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (! isExit) {
isExit = true;
Toast.makeText(getApplicationContext(), "Press again to exit the program",
Toast.LENGTH_SHORT).show();
// Use handler to delay sending Change status information
mHandler.sendEmptyMessageDelayed(0, 2000);
} else {
exit(this);
}
return false;
}
return super.onKeyDown (keyCode, event);}

2) Save the click time:

//Save the click time
private long exitTime = 0;
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if ((System.currentTimeMillis() - exitTime) > 2000) {
Toast. MakeText (getApplicationContext (), "Press another exit program",
Toast.Length_Short) .show ();
Exittime = System.Currenttimemillis ();
} ## exit ();
                 }
return false;
}
return super.onKeyDown(keyCode, event);
}

7. Set cutscene animation for Activity

The so-called cutscene animation is to add some switching animations when switching to another Activity, such as fade in and fade out, zoom in and out, push left and right, etc. ! Of course, we do not explain animation in detail here. There will be a special chapter to explain this later. Here we only teach you how to load animation. In addition We provide you with some commonly used transition animations. Just add the relevant animation files to the res/anim directory, and then choose one of the following methods. You can realize the switching animation of Activity!

1) Method 1:

7.png

2) Method 2:

Configure through style, this is global , that is, all activities will load this animation!

The implementation code is as follows:

① Customize the style in style.xml:

<! -- Default Activity jump animation -->
<style name="default_animation" mce_bogus="1" parent="@android:style/Animation.Activity">
<item name=" android:activityOpenEnterAnimation">@anim/default_anim_in</item>
; <item name="android:activityOpenExitAnimation">@anim/anim_stay</item>
; <item name="android:activityCloseEnterAnimation" >@anim/anim_stay</item>
  <item name="android:activityCloseExitAnimation">@anim/default_anim_out</item>
</style>

Explanation:

The 4 items respectively represent:

  • Activity B enters animation when Activity A jumps to Activity B;
  • Activity A Activity A’s exit animation when jumping to Activity B;
  • Activity A’s entry animation when Activity B returns to Activity A
  • Activity B’s exit animation when Activity B returns to Activity A

②Then modify the AppTheme:

<style name="AppTheme" mce_bogus="1" parent="@android:style/Theme.Light"> ;
                                                                                                                                                                  style>

③Finally under the application settings:

<application
android:icon="@drawable/logo"
android:label="@ string/app_name"
android:theme="@style/AppTheme" >

Okay, the animation effects are set just like this~

3) Others

Okay, in addition to the above two methods, you can also use TransitionManager to achieve it, but the required version is API 19 or above. There is also a conversion animation of addOnPreDrawListener, which is a bit troublesome to use and may not be suitable for beginners. I won’t go into details here. Finally, we provide some commonly used animation effect packages. Just select the required special effects and add them to the project! Activity commonly used transition animation.zip


8. Limitations of Bundle data transfer

When using Bundle to transfer data, please note that the size of Bundle is limited< 0.5MB, if greater than this value A TransactionTooLargeException exception will be reported! ! !


9. Use the command line to view the commands of all current activities:

Use the following commands, provided that you have configured environment variables for the SDK: adb shell dumpsys activity


10. How to set Activity full screen:

1) Code hiding ActionBar

in Activity Call getActionBar.hide(); in the onCreate method.

2) Set via requestWindowFeature

requestWindowFeature(Window.FEATURE_NO_TITLE); This code needs to be called before setContentView (), otherwise an error will be reported! ! !

Note: Put requestWindowFeature(Window.FEATURE_NO_TITLE); in front of super.onCreate(savedInstanceState); to hide the ActionBar without reporting an error.

3) Set theme = @android:style/Theme.NoTitleBar.FullScreen

in the label of the Activity that requires full screen through the theme


of AndroidManifest.xml

11. The wonderful use of the onWindowFocusChanged method:

Let’s first take a look at the official introduction to this method:

That is, when the Activity gets or loses focus, this method will be called back! If we want to monitor whether the Activity is loaded, we can use this method~ If you want to learn more about it, you can go to this article: Introduction to onWindowFocusChanged trigger


12. Define dialog-style Activity

In some cases, we may need Set the Activity to dialog style. The Activity usually takes up the full screen. The Dialog takes up part of the screen! It’s also easy to implement!

Set directly the theme of Activity:

android:theme="@android:style/Theme.Dialog"

That’s it. Of course you can set the title and small icon!


//Set the small icon in the upper left corner
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.main);
getWindow().setFeatureDrawableResource( Window.FEATURE_LEFT_ICON, android.R.drawable.ic_lion_icon);
//Set text:
setTitle(R.string.actdialog_title); //Set in XML code: android:label="@string/activity_dialog"

Summary of this section:

Okay, in this section we have learned some common problems in the actual development of Activity. I believe it will be helpful in actual development. Ours! In the next section, we will learn the concept of Activty's stack and the four loading modes! Stay tuned~Thank you~