Android 中 Activity 启动模式(singleInstance、singleTask、singleTop、standard)

任务栈

第一课时:

启动模式的配置:

标准启动模式:



MainActivity :
package com.example.launch;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
    private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView) findViewById(R.id.textView1);
textView.setText(String.format("TaskID:%d\nCurrent Activity id %s", getTaskId(),toString()));
   findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
@Override

});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}





MainActivity.java:




package com.example.launch;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends Activity {
    private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView) findViewById(R.id.tv);
textView.setText(String.format("TaskID:%d\nCurrent Activity id %s", getTaskId(),toString()));
   findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,MainActivity.class));
}
});
   findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,SecondMainActivity.class));
}
});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}



SecondMainActivity.java:





package com.example.launch;


import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;


public class SecondMainActivity extends Activity {
    private  TextView  textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_main);
textView=(TextView) findViewById(R.id.tv);
textView.setText(String.format("TaskID:%d\nCurrent Activity id %s", getTaskId(),toString()));
findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(SecondMainActivity.this,MainActivity.class));
}
});
   findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(SecondMainActivity.this,SecondMainActivity.class));
}
});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.second_main, menu);
return true;
}


}





标准启动模式

你可能感兴趣的:(Android 中 Activity 启动模式(singleInstance、singleTask、singleTop、standard))