Android测试活动singleTop启动模式

木来 木来

(1)在Androidmanifest.xml中注册活动为singleTop模式

<activity android:name=".MainActivity"    android:launchMode="singleTop"    >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

(2)在MainActivity中编写代码,查看控制台效果

package com.shihsf.hello;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {


    private static final String TAG = "MainActivity";





    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.e(TAG,this.toString());










        setContentView(R.layout.activity_main);

        Button button1=findViewById(R.id.normal_button);


        button1.setOnClickListener(new View.OnClickListener() {
            @Override            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });












    }
}
0 条评论