Android四大组件面试Activity

Activity

activity生命周期

1.四种状态

running/paused/stopped/killed

2.activity生命周期分析

​ (1) onCreate()—>onStart()—>onResume()—>onPause()—>Onstop()—>OnDestory

​ (2) onCreate()—>onStart()—>onResume()—>onPause()—>onResume()—>……

​ (3) onCreate()—>onStart()—>onResume()—>onPause()—>Onstop()—>onRestart()—>onStart()……

  • onStart()后,界面可见,但不能与用户交互;
  • onResume()可以与用户交互;
  • onPause()用户可见或者部分可见,但失去焦点,不能与用户交互;
  • onStop()用户不可见;

3.activity进程优先级

  • 前台进程
  • 可见进程
  • 服务进程
  • 后台进程
  • 空进程

activity任务栈

后进先出;任务栈不是唯一的,可能有多个。

activity启动模式

  • standard:新建Activity的实例,加入到任务栈,即使存在原来的实例,不会复用。
  • singletop:栈顶复用
  • singletask:栈内复用,复用这个实例,并将其上的Activity移除出栈。
  • singleinstance:整个系统内只有一个实例,且其独享一个任务栈。

activity间传递数据

  1. 通过Intent传递数据

    • Intent 和Bundle 设置数据
 Intent intent =new Intent(CurrentActivity.this,OtherActivity.class);
  // 创建一个带“收件人地址”的 email 
 Bundle bundle =new Bundle();// 创建 email 内容
 bundle.putBoolean("boolean_key", true);// 编写内容
 bundle.putString("string_key", "string_value"); 
 intent.putExtra("key", bundle);// 封装 email 
 startActivity(intent);// 启动新的 Activity
  • Intent和Bundle获取数据
Intent intent =getIntent();// 收取 email 
Bundle bundle =intent.getBundleExtra("key");// 打开 email 
bundle.getBoolean("boolean_key");// 读取内容
bundle.getString("string_key");
  • Intent 设置 获取数据
//====================设置数据=====================================
Intent intent =new Intent(EX06.this,OtherActivity.class); 
intent.putExtra("boolean_key", true); 
intent.putExtra("string_key", "string_value"); 
startActivity(intent);
//====================获取数据=====================================
Intent intent=getIntent(); 
intent.getBooleanExtra("boolean_key",false); 
intent.getStringExtra("string_key");
  1. 通过静态变量传递数据

适用于不可序列化的且简单的对象

  1. 通过全局对象(Application)传递数据

如果想使某些数据长时间驻留内存,以便程序随时调用,建议采用全局对象。Application全局类不需要定义静态变量只要定义普通成员变量即可,但全局类中必须有一个无参构造方法.

  • Application全局对象
public class MainApplication extends Application {
   private String username;
   public String getUsername() {
       return username;
   }
   public void setUsername(String username) {
       this.username = username;
   }
}
  • 设置数据
public class MainActivity extends Activity {
   private MainApplication application;
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       application = (MainApplication) getApplication();
       application.setUsername("sunzn");
   }
   public void open(View view) {
       Intent intent = new Intent(this, OtherActivity.class);
       startActivity(intent);
   }
}
  • 获取数据
public class OtherActivity extends Activity {
   private TextView tv_data;
   private MainApplication application;
   private String username;
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_other);
       application = (MainApplication) getApplication();
       username = application.getUsername();
       tv_data = (TextView) findViewById(R.id.tv_data);
       tv_data.setText("从上一个 Activity 中获取到的数据为:" + username);
   }
}

从Activity返回数据

Activity中的相关方法:

  • startActivityForResult(Intent intent, int requestCode):If >= 0, this code will be returned in onActivityResult() when the activity exits.
  • onActivityResult(int requestCode, int resultCode, Intent data):requestCode,在startActivityForResult中设置的,用于区别这个结果的来源;resultCode:在子Activity中通过setResult()设置并返回的。

例子:从Activity1跳转到Activity2,从Activity2返回数据到Aactivity1

  • 从Activity1跳转到Activity2
Intent intent = new Intent();
intent = intent.setClass(ActivityIntent.this, AnotherActivity.class);
Bundle bundle = new Bundle();
bundle.putString("string", et_string.getText().toString());
intent.putExtras(bundle);
startActivityForResult(intent,0); //只有这里不同
  • 从Activity2返回数据到Aactivity1
Intent intent = new Intent();
intent = intent.setClass(AnotherActivity.this, ActivityIntent.class);
Bundle bundle = new Bundle();
bundle.putInt("result", "Activity2的处理结果");
intent.putExtras(bundle); 
AnotherActivity.this.setResult(RESULT_OK, intent); //RESULT_OK是返回状态码
AnotherActivity.this.finish();
  • 在Activity1中重写onActivityResault方法,接收数据:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch(resultCode) { //根据状态码(在startActivityForResult中设置的),处理返回结果
         case RESULT_OK: 
            Bundle bundle =data.getExtras(); //获取intent里面的bundle对象
            String result = bundle.getInt("result"); 
          break; 
          default:
          break;
 } 

状态保存

onSaveInstanceState(Bundle outState),系统决定是否调用此方法。如果调用,此方法将在onStop之前发生,不保证是否会在onPause之前或之后发生。

public class MainActivity extends Activity {  
    private String temp;    
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        // 从savedInstanceState中恢复数据, 如果没有数据需要恢复savedInstanceState为null  
        if (savedInstanceState != null) {  
            temp = savedInstanceState.getString("temp");  
            System.out.println("onCreate: temp = " + temp);  
        }  
    }  

    public void onRestoreInstanceState(Bundle saveInstanceState) {  
        super.onRestoreInstanceState( saveInstanceState);  
        String temp  = saveInstanceState.getString("temp");  
        System.out.println("onResume: temp = " + temp);  

    }     
    // 将数据保存到outState对象中, 该对象会在重建activity时传递给onCreate方法和onRestoreInstanceState方法
    @Override  
    protected void onSaveInstanceState(Bundle outState) {  
        super.onSaveInstanceState(outState);  
        outState.putString("temp", temp);  
    }

你可能感兴趣的:(Android)