Activity学习认知

来源:互联网 发布:微星知乎 编辑:程序博客网 时间:2024/06/01 10:29

1.activity生命周期


The entire lifecycle of an activity is defined by the following Activity methods. All of these are hooks that you can override to do appropriate work when the activity changes state. All activities will implement onCreate(Bundle) to do their initial setup; many will also implement onPause() to commit changes to data and otherwise prepare to stop interacting with the user. You should always call up to your superclass when implementing these methods.

翻译:activity的全部生命周期通过以下几个方法定义。当activity的状态变化时你可以根据功能来重写相应的方法。所有的activity必须实现onCreate(Bundle) 来初始化;当打算提交更改的数据或者停止与用户的互动是可能需要重写onPause() 方法。当重写这些方法时你必须调用父类的方法。

2.activity间的跳转和值传递

    显式跳转

public class MainActivity extends Activity {private EditText nameEt;private Button ceshiBtn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);nameEt = (EditText)findViewById(R.id.nameEt);ceshiBtn = (Button)findViewById(R.id.ceshiBtn);Intent intent = new Intent(this,RpCaculatorActivity.class);startActivity(intent);                //startActivityForResult(intent, 1000);}}

   隐跳转

public class MainActivity extends Activity {private EditText nameEt;private Button ceshiBtn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);nameEt = (EditText)findViewById(R.id.nameEt);ceshiBtn = (Button)findViewById(R.id.ceshiBtn);Intent intent = new Intent("com.example.rpcaculator.test2");                startActivity(intent);                //startActivityForResult(intent, 1000);}}

如果通过startActivityForResult这个方法进行activity跳转的可以获取传回值。

值传递:1、直接通过intent的自带的方法进行值传递,其传值可以使字符串、对象、list等具体可以查看其方法。

                       如:Intent intent = new Intent(this,RPCacultorActivity.class);

                               intent.intent.putExtra("name", "1111");

                               在下一个activity中可以通过以下方式获得

                                Intent intent = this.getIntent();
                String name = intent.getStringExtra("name");

                 2、通过Bundle进行数据传递

                       如:Intent intent = new Intent(this,RPCacultorActivity.class);

                               Bundle data = new Bundle();

                               data.putString("name","liu");

                               intent.putExtras(data);

                               在下一个activity中通过以下方式获得相应值:

                               Bundle data = this.getIntent().getExtras();
                String name = data.getString("name");



0 0
原创粉丝点击