Android页面跳转

Android页面跳转大概有三种情形:

 1.从A跳转到B,不带数据。

 2.从A跳转到B,带数据。

 3.从A跳转到B,A接受B返回的数据且做相应处理。

 第一种和第二种情形非常简单,下面主要介绍第三种情形:

   在介绍之前先将第三种情形的大致流程说一下, 使用startActivityForResult(Intent intent, int requestCode)方法打开新的Activity,然后新的Activity使用setResult(int resultCode)只返回结果不带数据,或者setResult(int resultCode, Intent data)两者都返回!当这个新Activity关闭后,系统都会调用前面Activity的onActivityResult(int requestCode, int resultCode, Intent data)方法。

 

requestCode,请求码:请求码的值是根据业务需要由自已设定,用于标识请求来源。

例如:一个Activity有两个按钮,点击这两个按钮都会打开同一个Activity,不管是那个按钮打开新Activity,当这个新Activity关闭后,系统都会调用前面Activity的onActivityResult(int requestCode, int resultCode, Intent data)方法。使用startActivityForResult(Intent intent, int requestCode)方法打开新的Activity在onActivityResult()方法如果需要知道新Activity是由那个按钮打开的,并且要做出相应的业务处理。

 

resultCode,结果码:在一个Activity中,可能会使用startActivityForResult()方法打开多个不同的Activity处理不同的业务,当这些新Activity关闭后,系统都会调用前面Activity的onActivityResult(int requestCode, int resultCode, Intent data)方法。为了知道返回的数据来自于哪个新Activity,在onActivityResult()方法中可以这样做(ResultActivity和NewActivity为要打开的新Activity):

 

Myapp.java
public   class  Myapp  extends  Activity {
    
private  Button buttonNewInput;
    
private  TextView displayContent;

    @Override
    
protected   void  onCreate(Bundle savedInstanceState) {
        
super .onCreate(savedInstanceState);
        setContentView(R.layout.main);
        buttonNewInput 
=  (Button)findViewById(R.id.buttonNewInput);
        displayContent 
=  (TextView)findViewById(R.id.displayContent);
        
        buttonNewInput.setOnClickListener(
new  OnClickListener() {
            
            @Override
            
public   void  onClick(View v) {
                Intent intent 
=   new  Intent();
                intent.setClass(Myapp.
this , Input. class );
                startActivityForResult(intent, 
0 );
            }
        });
    }

    @Override
    
protected   void  onActivityResult( int  requestCode,  int  resultCode, Intent data) {
        
if (requestCode  ==   0 ) {
            
if (resultCode  ==  Activity.RESULT_OK) {
                SharedPreferences preferences 
=  getSharedPreferences( " Text " 0 );
                displayContent.setText(preferences.getString(
" text " null ));
            }
        }
//         super.onActivityResult(requestCode, resultCode, data);
    }

 

 

main.xml
<? xml version = " 1.0 "  encoding = " utf-8 " ?>
< LinearLayout xmlns:android = " http://schemas.android.com/apk/res/android "
    android:orientation
= " vertical "
    android:layout_width
= " fill_parent "
    android:layout_height
= " fill_parent "
    
>
< TextView  
    android:layout_width
= " fill_parent "  
    android:layout_height
= " wrap_content "  
    android:text
= " @string/hello "
    android:id
= " @+id/displayContent " />
< Button 
    android:layout_height = " wrap_content "  
    android:text = " 新窗口 "  
    android:layout_width = " wrap_content "  
    android:id = " @+id/buttonNewInput " ></ Button >
</ LinearLayout >

 

 

Input.java
public   class  Input  extends  Activity {
    
private  EditText editTextInput;
    
private  Button buttonInputOK; 

    @Override
    
protected   void  onCreate(Bundle savedInstanceState) {
        
super .onCreate(savedInstanceState);
        setContentView(R.layout.input);
        editTextInput 
=  (EditText)findViewById(R.id.editTextInput);
        buttonInputOK 
=  (Button)findViewById(R.id.buttonInputOK);
        buttonInputOK.setOnClickListener(
new  OnClickListener() {
            
            @Override
            
public   void  onClick(View v) {
                String text 
=  editTextInput.getText().toString();
                SharedPreferences preferences 
=  getSharedPreferences( " Text " 0 );
                SharedPreferences.Editor editor 
=  preferences.edit();
                editor.putString(
" text " , text);
                
                
if (editor.commit()) {
                    setResult(Activity.RESULT_OK);
                }
                finish();
             }
        });
    }
    
}

 

 

input.xml
<? xml version = " 1.0 "  encoding = " utf-8 " ?>
< LinearLayout
  xmlns:android
= " http://schemas.android.com/apk/res/android "
  android:layout_width
= " fill_parent "
  android:layout_height
= " fill_parent "  android:orientation = " vertical " >
    
< EditText 
          android:layout_height = " wrap_content "  
         android:layout_width = " fill_parent "  
         android:id = " @+id/editTextInput " ></ EditText >
    
< Button 
         android:layout_width = " wrap_content "  
         android:layout_height = " wrap_content "  
         android:text = " 确定 "  android:id = " @+id/buttonInputOK " ></ Button >
</ LinearLayout >
 
Android页面跳转
 
 
Android页面跳转

你可能感兴趣的:(android)