4.1.2 A Glimpse into Activities
Category Android Basic Tutorial
Introduction to This Section:
In the previous section, we learned some basic concepts about Activities, such as what an Activity is, the lifecycle of an Activity, and how to launch an Activity. In this section, we will continue to learn about Activities. As mentioned earlier, an app is usually composed of multiple Activities, which leads to the issue of data transmission between multiple Activities. So, this section will continue to explore the use of Activities! In addition, the transmission of collections, objects, arrays, and Bitmaps will be discussed in the Intent section, and here we will only introduce how to pass basic data!
1. Data Transmission Between Activities:
Code Example:
Effect Picture:
Code Download: ActivityTest1.zip
2. Interaction Between Multiple Activities (Passing Back from the Last to the First)
Code Example:
Effect Picture:
Code Download: ActivityTest2.zip
3. Knowing Which Activity is Currently Active
4. Closing All Activities at Any Time
>
Sometimes we may have opened many Activities, and suddenly there is a requirement to close all of them and exit the program on a certain page! Well, a method to close all Activities is provided below, which is to use a list 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()
{
for(Activity activity:activities)
{
if(!activity.isFinishing())
{
activity.finish();
}
}
}
}
5. Method to Completely Exit the App
The above is about closing all Activities, but sometimes we may want to kill the entire App, including background tasks. If you want to kill everything cleanly, you can use the following code:
Implementation Code:
/**
* Exit the application
*/
public void AppExit(Context context) {
try {
ActivityCollector.finishAll();
ActivityManager activityMgr = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
activityMgr.killBackgroundProcesses(context.getPackageName());
System.exit(0);
} catch (Exception ignored) {}
}
6. Two Methods for Exiting the Program with a Double Tap:
1) Define a variable to indicate whether to exit
// Define a variable to indicate whether to 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",
Toast.LENGTH_SHORT).show();
// Use handler to delay sending a message to change the state
mHandler.sendEmptyMessageDelayed(0, 2000);
} else {
exit(this);
}
return false;
}
return super.onKeyDown(keyCode, event);}
2) Save the tap time:
// Save the tap 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 again to exit",
Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else {
exit();
}
return false;
}
return super.onKeyDown(keyCode, event);
}
7. Setting Transition Animations for Activities
>
The so-called transition animation is to add some switching animations when switching to another Activity, such as fade in and out, zoom in and out, push left and right, etc.! Of course, we do not explain the animation in detail here, and there will be a special chapter to explain this later. Here, we only teach you how to load the animation, and also provide This code needs to be called before setContentView(), otherwise an error will occur!!!
>
Note: Placing requestWindowFeature(Window.FEATURE_NO_TITLE); before super.onCreate(savedInstanceState); can hide the ActionBar without causing an error.
3) Through the theme in AndroidManifest.xml
Set theme = @android:style/Theme.NoTitleBar.FullScreen within the tag of the Activity that requires full screen.
11. The ingenious use of the onWindowFocusChanged method:
Let's first look at the official introduction to this method:
That is, when the Activity gains or loses focus, this method will be called back! If we want to monitor whether the Activity has been loaded, we can use this method~ For those who want to delve deeper, please visit this article: Introduction to onWindowFocusChanged Trigger
12. Defining an Activity with a dialog style
>
In some cases, we may need to set the Activity to a dialog style. An Activity usually fills the entire screen, while a Dialog occupies only part of the screen! The implementation is also very simple!
Just set the theme of the Activity directly:
android:theme="@android:style/Theme.Dialog"
//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 the text:
setTitle(R.string.actdialog_title); //In XML code: android:label="@string/activity_dialog"
Summary of this section:
Well, in this section, we have learned about some common issues of Activity in actual development, which I believe will help everyone in actual development! In the next section, we will learn about the concept of the Activity stack, as well as the four loading modes! Stay tuned~ Thank you~
-1.0 Android Basic Tutorial Introduction
-1.0.1 Latest Android Basic Tutorial Catalog for 2015
-1.1 Background and System Architecture Analysis
-1.2 Development Environment Setup
-1.2.1 Developing Android APP with Eclipse + ADT + SDK
-1.2.2 Developing Android APP with Android Studio
-1.3 Solving SDK Update Issues
-1.4 Genymotion Emulator Installation
-1.5.1 Git Tutorial on Basic Operations of Local Repositories
-1.5.2 Git on Setting Up Remote Repositories with GitHub
-1.6 How to Play with 9 (Jiu Mei) Images
-1.7 Interface Prototype Design
-1.8 Project Related Analysis (Various Files, Resource Access)
-1.9 Android Program Signing and Packaging
-1.11 Decompiling APK to Obtain Code & Resources
-2.1 The Concept of View and ViewGroup
-2.2.1 LinearLayout (Linear Layout)
-2.2.2 RelativeLayout (Relative Layout)
-2.2.3 TableLayout (Table Layout)
-2.2.4 FrameLayout (Frame Layout)
-2.2.5 GridLayout (Grid Layout)
-2.2.6 AbsoluteLayout (Absolute Layout)
-2.3.1 TextView (Text Box) Detailed Explanation
-2.3.2 EditText (Input Box) Detailed Explanation
-2.3.3 Button (Button) and ImageButton (Image Button)
-2.3.5 RadioButton (Radio Button) & Checkbox (Checkbox)
-[2.3
4.1.2 Activity Introduction
4.4.2 Further Exploration of ContentProvider - Document Provider
5.2.1 Detailed Explanation of Fragment Example - Implementation of Bottom Navigation Bar (Method 1)
5.2.2 Detailed Explanation of Fragment Example - Implementation of Bottom Navigation Bar (Method 2)
5.2.3 Detailed Explanation of Fragment Example - Implementation of Bottom Navigation Bar (Method 3)
5.2.4 Detailed Explanation of Fragment Example - Bottom Navigation Bar + ViewPager Page Switching
6.2 Data Storage and Access - SharedPreferences for Saving User Preferences
7.1.1 What to Learn in Android Network Programming and Http Protocol Study
[8.1.3 Summary of 13 Android Drawables Part 3]
11.0 "2015 Latest Android Basic Tutorial" Concludes with Cheers~
12.2 DrySister Girl Viewing App (First Edition) – 2. Parsing Backend Data
12.4 DrySister Girl Viewing App (First Edition) – 4. Adding Data Caching (Integrating SQLite)
12.5 DrySister Girl Viewing App (First Edition) – 5. Code Review, Adjustment, and Log Class Writing