1、Activity拥有可以编辑用户界面的窗口,窗口布满整个屏幕,也可以小于或者漂浮于其他窗口之上
2、应用程序通常拥有多个互相关联的Activity和一个MainActivity
多个Activity之间跳转简单示意
package com.songjialongs.myapplication; import android.os.PersistableBundle; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; public class Activity_Lifecycle extends AppCompatActivity { EditText user_name_et; CheckBox rem_user_name_cb; boolean iscckeck = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity__lifecycle); user_name_et = (EditText) findViewById(R.id.user_name_et); rem_user_name_cb = (CheckBox) findViewById(R.id.rem_user_name_cb); iscckeck = savedInstanceState.getBoolean("remname"); rem_user_name_cb.setChecked(iscckeck); final String name = savedInstanceState.getString("name"); rem_user_name_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean b) { if (b) { user_name_et.setText(name); iscckeck = true; } else { iscckeck = false; } } }); } @Override public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { super.onSaveInstanceState(outState, outPersistentState); String content = user_name_et.getText().toString(); outState.putString("name", content);//参数为String Key,String Value outState.putBoolean("remname", iscckeck);//参数为String Key,boolean Value } }layout代码:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/user_name_txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:text="用户名:" android:textSize="32sp" /> <EditText android:id="@+id/user_name_et" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:layout_marginTop="20dp" android:layout_toRightOf="@id/user_name_txt" /> <CheckBox android:id="@+id/rem_user_name_cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@id/user_name_et" android:layout_below="@id/user_name_et" android:text="记住密码" android:textSize="24sp" /> </RelativeLayout>
package com.songjialongs.myapplication.LuancherMode; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.songjialongs.myapplication.R; public class MainActivity extends AppCompatActivity { TextView activty_A_Txt; Button activty_A_Btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity__main); activty_A_Txt = (TextView) findViewById(R.id.activty_a_txt); activty_A_Btn = (Button) findViewById(R.id.activity_a_btn); activty_A_Txt.setText(this.toString()); activty_A_Btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, Activity_Stardard_B.class); startActivity(intent); } }); } }
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.songjialongs.myapplication.R; public class Activity_Stardard_B extends AppCompatActivity { TextView activty_B_Txt; Button activty_B_Btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity__stardard); activty_B_Txt = (TextView) findViewById(R.id.stardard_b_txt); activty_B_Btn = (Button) findViewById(R.id.stardard_b_btn); activty_B_Txt.setText(this.toString()); activty_B_Btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(Activity_Stardard_B.this, MainActivity.class); startActivity(intent); } }); } }
package com.songjialongs.myapplication.LuancherMode; import android.app.Activity; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.songjialongs.myapplication.R; public class MainActivity extends AppCompatActivity { TextView activty_A_Txt; Button activty_A_Btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity__main); activty_A_Txt = (TextView) findViewById(R.id.activty_a_txt); activty_A_Btn = (Button) findViewById(R.id.activity_a_btn); activty_A_Txt.setText(this.toString()); activty_A_Btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, Activity_SingleTop_B.class); startActivity(intent); } }); } }
package com.songjialongs.myapplication.LuancherMode; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.songjialongs.myapplication.R; public class Activity_SingleTop_B extends AppCompatActivity { TextView singletop_B_Btn_Txt; Button singletop_B_Btn_btn; Button singletop_BtoA_Btn_Txt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity__singletop__b); singletop_B_Btn_Txt = (TextView) findViewById(R.id.singletop_b_btn_txt); singletop_B_Btn_btn = (Button) findViewById(R.id.singletop_b_btn); singletop_BtoA_Btn_Txt = (Button) findViewById(R.id.singletop_btoa_btn); singletop_B_Btn_Txt.setText(this.toString()); singletop_B_Btn_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(Activity_SingleTop_B.this, Activity_SingleTop_B.class); startActivity(intent); } }); singletop_BtoA_Btn_Txt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(Activity_SingleTop_B.this, MainActivity.class); startActivity(intent); } }); } }
package com.songjialongs.myapplication.LuancherMode; import android.app.Activity; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.songjialongs.myapplication.R; public class MainActivity extends AppCompatActivity { TextView activty_A_Txt; Button activty_A_Btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity__main); activty_A_Txt = (TextView) findViewById(R.id.activty_a_txt); activty_A_Btn = (Button) findViewById(R.id.activity_a_btn); activty_A_Txt.setText(this.toString()); activty_A_Btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, Activity_SingleTask_B.class); startActivity(intent); } }); } }
package com.songjialongs.myapplication.LuancherMode; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.songjialongs.myapplication.R; public class Activity_SingleTask_B extends AppCompatActivity { TextView activty_B_Txt; Button activty_B_Btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity__singletask); activty_B_Txt = (TextView) findViewById(R.id.singletask_b_txt); activty_B_Btn = (Button) findViewById(R.id.singletask_b_btn); activty_B_Txt.setText(this.toString()); activty_B_Btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(Activity_SingleTask_B.this, MainActivity.class); startActivity(intent); } }); } }
import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.songjialongs.myapplication.R; public class MainActivity extends AppCompatActivity { TextView activty_A_Txt; Button activty_A_Btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); activty_A_Txt = (TextView) findViewById(R.id.activty_a_txt); activty_A_Btn = (Button) findViewById(R.id.activity_a_btn); activty_A_Txt.setText(this.toString() + ":栈:" + getTaskId()); activty_A_Btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity.this, Activity_SingleInstance_B.class); startActivity(intent); } }); } }
package com.songjialongs.myapplication.LuancherMode; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.songjialongs.myapplication.R; public class Activity_SingleInstance_B extends AppCompatActivity { TextView singinstance_B_Txt; Button singinstance_B_Btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity__singleinstance); singinstance_B_Txt = (TextView) findViewById(R.id.singleinstance_b_txt); singinstance_B_Btn = (Button) findViewById(R.id.singleinstance_b_btn); singinstance_B_Txt.setText(this.toString() + ":栈:" + getTaskId()); singinstance_B_Btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(Activity_SingleInstance_B.this, Activity_SingleInstance_B.class); startActivity(intent); } }); } }