Android之Activity

1.跳转到另一个Activity

1.在activity_main中创建一个btn,并且改其id为pushBtton
2.新建一个Activity
3.在MainActivity中添加如下代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });

    //添加的代码
    findViewById(R.id.pushbutton).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(MainActivity.this,AnotherAtr.class));
        }
    });

}


2.Activity之生命周期

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        System.out.println("onCreat");
    }

    @Override
    protected void onStart() {
        super.onStart();
        System.out.println("onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        System.out.println("onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        System.out.println("onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        System.out.println("onDestory");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        System.out.println("onRestart");
    }


}


3.Activity之间传递参数

1>传递简单数据

//1.在Main和NewActivity的layout各自建立一个btn和textView,并且设置其id
//2.在MainActivity中
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.pusnButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent i = new Intent(MainActivity.this,ReceiveAct.class);
                i.putExtra("data","post nsnotificaition");//传值
                startActivity(i);
            }
        });
    }
}
//3.在NewActivity中
public class ReceiveAct extends AppCompatActivity {

    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive);

        Intent i = getIntent();
        tv = (TextView)findViewById(R.id.myId);
        tv.setText(i.getStringExtra("data"));//接收值
    }
}

2>传递数据包

//1.Main上
Bundle b = new Bundle();
//i.putExtra("data",b);
i.putExtras(b);

//2,New上
// Bundle data = i.getBundleExtra("data")
 Bundle data = i.getExtras();

3>传递对象

//1.传递的class要序列化
public class User implements Serializable{

    private String name;
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public  User(String name,int age){
        this.name = name;
        this.age = age;
    }

}
//2.Main
i.putExtra("user",new User("Tom",18));
startActivity(i);
//3.New
User user = (User) i.getSerializableExtra("user");//输出序列号格式,并且强转
tv.setText(String.format("name = %s age = age",user.getName(),user.getAge()));

4>.获取Activity的返回参数

//1.首先惯例Main跳转,New添加EditText,backBtn
//2.New上
public class ReceiveAct extends AppCompatActivity {
    private EditText editText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive);

        editText = (EditText)findViewById(R.id.editText);
        findViewById(R.id.backButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = getIntent();
                i.putExtra("data",editText.getText().toString());
                setResult(1,i);//1.是状态吗,自定义。
                finish();
            }
        });


    }
}
//3.Main上接收数据
public class MainActivity extends AppCompatActivity {

    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView)findViewById(R.id.textView);

        findViewById(R.id.pusnButton).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent i = new Intent(MainActivity.this,ReceiveAct.class);
               // startActivity(i);
                startActivityForResult(i,0);//必须使用能新接收结果的start方法
            }
        });
    }
    //接收结果的代理方法
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        
        textView.setText(data.getStringExtra("data"));

    }
}


4.Android中的Activity启动模式

1>.标准启动模式 standard

tv = (TextView)findViewById(R.id.textView);
tv.setText(String.format("TaskID:%s \n current Activity:%s",getTaskId(),toString()));

findViewById(R.id.pushBtn).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        startActivity(new Intent(MainActivity.this,MainActivity.class));
    }
});

2>.singleTol

跳转前后,任务栈都是一样的。如果处于栈顶(例如自己跳转到自己),就不会创建实例

3>.singleTask与singleInstance

singleTask:Main跳到New,此时,内存就有Main和New两个界面。New再跳Main,New就会直接弹出栈,不在内存中了。

singleInstance:Main跳到New,任务栈发生改变。当New跳到Main,任务栈也发生变化,所以独立任务栈只放一个实例。两者来回切换


5.创建全新的Activity

//1.创建一个layout

//2.先创建好一个class,继承Activity。再创建onCreate方法
public class AnotherAtr extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.another_atr);//
    }
}

//3.在AndoridMainifest中添加新的Activity的class名字
<activity android:name=".AnotherAtr"></activity>


你可能感兴趣的:(Android之Activity)