美文网首页
WD的Activity学习

WD的Activity学习

作者: singleLine0 | 来源:发表于2018-11-04 00:59 被阅读0次

大多为《第一行代码代码》内容

1. Menu用法

[1] 在Menu目录下定义main.xml文件

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/add_item"
        android:title="Add"></item>
    <item
        android:id="@+id/remove_item"
        android:title="Remove"></item>
</menu>

[2] 重写onCreateOptionsMenu(),使用inflate导入main.xml

public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }

[3] 重写onOptionsItemSelected(),写入点击事件

 public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.add_item:
                Toast.makeText(FirstActivity.this,"you clicked add",Toast.LENGTH_LONG).show();
                break;
            case R.id.remove_item:
                Toast.makeText(FirstActivity.this,"you clicked remove",Toast.LENGTH_LONG).show();
                break;
                default:
        }
        return true;
    }

2. 显式启动和隐式启动

[1] 显式 new Intent(A.this,B.class)

Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivity(intent);

[2] 隐式 manifest中通过intent-filter 定义属性action和category来确定Activity

           <intent-filter>
                <action android:name="type.anything"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
            </intent-filter>
                Intent intent1 = new Intent("type.anything");
                startActivity(intent1);

顾名思义,显式可以在代码中看到切换的Activity,隐式要和intent-filter匹配才能跳转

3. Activity间数据的传递

[1] 传递给下一个Activity

        intent.putExtra("data","data_return"); 
        Intent intent = getIntent();
        mString = intent.getStringExtra("data");

[2] 返回数据给上一个Activity

  • A: startActivityForResult() 启动下一个Activity
                startActivityForResult(intent,1);

onActivityResult() 处理返回的数据

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 1:
                if(resultCode == RESULT_OK) {
                    String string = data.getStringExtra("data");
                    Toast.makeText(FirstActivity.this,string,Toast.LENGTH_SHORT).show();
                    Log.e("FirstActivity", "onActivityResult: "+string);
                }
                break;
            default:
        }
    }
  • B: setResult() 放入返回给A的数据,通常要重写onBackPressedzhon
 public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra("data","data_return");
        setResult(RESULT_OK,intent);
        finish();
    }

4. Activity数据的保存

[1] 重写onSaveInstanceState() 方法

@Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        String tempData = "something just typed";
        outState.putString("temp",tempData);
    }

[2] 在onCreate()中判断savedInstanceState != null,并取出数据

 if(savedInstanceState != null) {
            String tempData = savedInstanceState.getString("temp");
        }

5. Activity启动方式

[1] Standard
[2] singleTop
[3] singTask
[4] singleInstance

6. Activity生命周期

[1] onCreate→onStart→onResume→onPause→onStop→onDestroy
[2] A跳转到B的过程:
A.onPause→B.onCreate→B.onStart→B.onResume →A.onStop
[3] 总是成对出现的方法: resume 和pause

7. 与Fragment的生命周期关系

8. 与View生命周期的关系

9. Activity启动过程

10. Activity finish()对成员变量和子线程影响

11. Activity进栈后,Http请求会如何

相关文章

网友评论

      本文标题:WD的Activity学习

      本文链接:https://www.haomeiwen.com/subject/ctnlxqtx.html