Solution to multiple Activity interfaces when android is clicked continuously
- 2021-11-29 08:37:26
- OfStack
Preface
At the beginning of learning android, I didn't know much about the startup mode of android, so I used time to judge whether to click the button twice and start another activity interface. This control method sometimes fails. For example, if another activity hasn't been started for two seconds, I can click again. Therefore, the startup mode of android is adjusted to control the repeated occurrence of multiple acitvity.
1. Control the number of clicks by time:
This method corresponds well to the control network request.
public class NoDoubleClickUtil {
private static long lastClickTime;
private final static int SPACE_TIME =2000;
public static void initLastClickTime() {
lastClickTime = 0;
}
public synchronized static boolean isDoubleClick() {
long currentTime = System.currentTimeMillis();
boolean isClickDouble;
if (currentTime - lastClickTime >
SPACE_TIME) {
isClickDouble = false;
} else {
isClickDouble = true;
}
lastClickTime = currentTime;
return isClickDouble;
}
}
2. Control how multiple activity appear through launchMode startup mode:
This method really eliminates the simultaneous occurrence of the same multiple activity.
< activity android:name=".InternetHospital.InternetHospitalInquiryCallUI"
android:launchMode="singleInstance"/ >
Or set in the code:
Intent intent = new Intent();
intent.setClass(getApplicationContext(), TargetActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
3. Four startup modes of android:
1. standard mode: A new Activity will be created and placed at the top of the stack every time startActivity () is called. (This mode is the default.)
2. singleTop mode: When starting Activity, specify that Activity will be created without being at the top of the stack, and if it is at the top of the stack, it will not be created again (two identical Activity adjoining will not appear)
3. singleTask mode: If the started Activity does not exist, create Activity, and if so, jump directly to the specified Activity (Activity above it will be moved out of the stack, that is, there cannot be duplicate Activity in one stack)
4. singleInstance mode: If the started Activity does not exist, create an Activity and create a stack at the same time. If it does, move the stack where the specified Activity exists to the top of the stack (indicating that this Activity can only exist in an independent task stack, which has nothing to do with other Activity applied)
Additional knowledge: Two classic methods of exiting multiple Activity in Android
1. Remember each activity with a set and then kill it one by one; Another way of thinking is to use broadcasting.
Method 1. Save the activity instance with list, and then kill it one by one
Create an external class to inherit Application to store activity
public class MyActvity extends Application {
// Create 1 A collection for storing activity Object of
ArrayList<Activity>list=new ArrayList<>();
// Declaration 1 Objects of this class
private static MyActvity instance;
public MyActvity() {
}
// Create 1 Methods used to initialize MyActivity And the object of the initialized object returns
public synchronized static MyActvity getInstance(){
if (instance==null){
instance=new MyActvity();
}
return instance;
}
// Calls this method to add to the collection activity Object
public void addActivity(Activity activity){
list.add(activity);
}
// Judge activity Is it already in the collection
public boolean isexitlist(Activity activity){
if (list.contains(activity)){
return true;
}
return false;
}
// When this method is called, close all activity
public void exit(){
for (Activity activity:list){
activity.finish();
}
// Exit the current MyActivity
System.exit(0);
}
@Override
public void onLowMemory() {
super.onLowMemory();
// When the storage space of the system is not enough, call the garbage recovery period of the system to clean up the garbage inside
System.gc();
}
}
Activity1 code:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Call deposit activity Class
MyActvity instance = MyActvity.getInstance();
// Judgement storage activity Class holds the activity There is no join class
if (!instance.isexitlist(this)){
instance.addActivity(this);
}
Intent intent = new Intent(this, Main2Activity.class);
startActivity(intent);
}
}
Activity2 code:
public class Main2Activity extends Activity {
private MyActvity instance;
/**
* Called when the activity is first created.
*/
@Override+
super.onCreate(savedInstanceState);
setContentView(R.layout.sdfa);
Button bt= (Button) findViewById(R.id.bt);
instance = MyActvity.getInstance();
if (!instance.isexitlist(this)){
instance.addActivity(this);
}
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Call exit () method to destroy all activity
instance.exit();
}
});
}
}
Method 2. Use the broadcast to register the broadcast in activity and start the broadcast when it is destroyed
Registered broadcast in MainActivity:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, Main2Activity.class);
startActivity(intent);
// Registered broadcast
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Define.PAHNAME);
registerReceiver(new MyReceiver(),intentFilter);
}
class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
unregisterReceiver(this);
((Activity)context).finish();
}
}
}
activity2: Start the broadcast
public class Main2Activity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sdfa);
Button bt= (Button) findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Define.PAHNAME);
sendBroadcast(intent);
finish();
}
});
}
}