zoukankan      html  css  js  c++  java
  • activity跳转后关闭父activity

      假如说有一个activity A,在A中跳转到Activity B, 然后在B中跳转到Activity C中,最后要在C中把activity A,B,C都关掉。

      当然网上说的有很多方法,其中有些方法是不安全的,我只说一种,不会对其他的代码造成影响就ok了。

      首先有一个Activity A,A中有一个Button,点击这个Button会跳到Activity B中去,但会拿到一个返回值,如果返回的是RESULT_OK的话就把Activity A也关掉。

      如下:

    1 package com.android.intent;
    2
    3  import android.app.Activity;
    4  import android.content.Intent;
    5  import android.graphics.Color;
    6  import android.os.Bundle;
    7  import android.view.View;
    8  import android.view.View.OnClickListener;
    9  import android.widget.Button;
    10
    11  public class A extends Activity {
    12
    13 private View a;
    14 private Button button;
    15
    16 @Override
    17 public void onCreate(Bundle savedInstanceState) {
    18 super.onCreate(savedInstanceState);
    19 setContentView(R.layout.a);
    20 a = findViewById(R.id.a);
    21 a.setBackgroundColor(Color.RED);
    22
    23 button = (Button) findViewById(R.id.aButton);
    24 button.setOnClickListener(btonClickListener);
    25 }
    26
    27 private OnClickListener btonClickListener = new OnClickListener() {
    28
    29 @Override
    30 public void onClick(View view) {
    31 // TODO Auto-generated method stub
    32   Intent intent = new Intent();
    33 intent.setClass(A.this, B.class);
    34 startActivityForResult(intent, 0);
    35 }
    36 };
    37
    38 @Override
    39 protected void onPause() {
    40 // TODO Auto-generated method stub
    41   super.onPause();
    42 System.out.println("activity A onPause");
    43 }
    44
    45 @Override
    46 protected void onDestroy() {
    47 // TODO Auto-generated method stub
    48   super.onDestroy();
    49 System.out.println("activity A onDestory");
    50 }
    51
    52 @Override
    53 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    54 // TODO Auto-generated method stub
    55   if(requestCode == 0 && resultCode == RESULT_OK) {
    56 finish();
    57 }
    58 }
    59 }

    A的布局文件,a.xml:

    1 <?xml version="1.0" encoding="utf-8"?>
    2  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3 android:orientation="vertical"
    4 android:layout_width="fill_parent"
    5 android:layout_height="fill_parent"
    6 android:id="@+id/a">
    7  <TextView
    8 android:layout_width="fill_parent"
    9 android:layout_height="wrap_content"
    10 android:text="I am activity A!"
    11 android:textSize="36sp"
    12 android:gravity="center"/>
    13 <Button
    14 android:layout_width="match_parent"
    15 android:layout_height="wrap_content"
    16 android:id="@+id/aButton"
    17 android:text="intent to activity B"/>
    18  </LinearLayout>

    同样有Activity B,和B的布局文件b.xml:

    1 <?xml version="1.0" encoding="utf-8"?>
    2  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3 android:orientation="vertical"
    4 android:layout_width="fill_parent"
    5 android:layout_height="fill_parent"
    6 android:id="@+id/b">
    7  <TextView
    8 android:layout_width="fill_parent"
    9 android:layout_height="wrap_content"
    10 android:text="I am activity B"
    11 android:textSize="36sp"
    12 android:gravity="center"/>
    13 <Button
    14 android:layout_width="match_parent"
    15 android:layout_height="wrap_content"
    16 android:id="@+id/bButton"
    17 android:text="intent to activity C"/>
    18  </LinearLayout>
    package com.android.intent;

    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class B extends Activity {

    private View b;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.b);
    b
    = findViewById(R.id.b);
    b.setBackgroundColor(Color.GREEN);

    button
    = (Button) findViewById(R.id.bButton);
    button.setOnClickListener(bButtonOnClickListener);
    }

    private OnClickListener bButtonOnClickListener = new OnClickListener() {

    @Override
    public void onClick(View view) {
    // TODO Auto-generated method stub
    Intent intent = new Intent();
    intent.setClass(B.
    this, C.class);
    startActivityForResult(intent,
    0);
    }
    };

    @Override
    protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    System.out.println(
    "activity B onPause");
    }

    @Override
    protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    System.out.println(
    "activity B onDestory");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    if(requestCode == 0 && resultCode == RESULT_OK) {
    setResult(RESULT_OK);
    finish();
    }
    }
    }

    还有Activity C 和它的布局文件c.xml,但它不做任何跳转,只想Activity B返回一个值,如何设置了这个返回值的话:

    1 <?xml version="1.0" encoding="utf-8"?>
    2  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3 android:orientation="vertical"
    4 android:layout_width="fill_parent"
    5 android:layout_height="fill_parent"
    6 android:id="@+id/c">
    7  <TextView
    8 android:layout_width="fill_parent"
    9 android:layout_height="wrap_content"
    10 android:text="I am acitivity C"
    11 android:textSize="36sp"
    12 android:gravity="center"/>
    13 <Button
    14 android:layout_width="match_parent"
    15 android:layout_height="wrap_content"
    16 android:id="@+id/cButton1"
    17 android:text="closeAll">
    18 </Button>
    19
    20 <Button
    21 android:layout_width="match_parent"
    22 android:layout_height="wrap_content"
    23 android:id="@+id/cButton2"
    24 android:text="close myself"/>
    25  </LinearLayout>
    1 package com.android.intent;
    2
    3 import android.app.Activity;
    4 import android.graphics.Color;
    5 import android.os.Bundle;
    6 import android.view.View;
    7 import android.view.View.OnClickListener;
    8 import android.widget.Button;
    9
    10 public class C extends Activity {
    11
    12 private View c;
    13 private Button button1;
    14 private Button button2;
    15
    16 @Override
    17 protected void onCreate(Bundle savedInstanceState) {
    18 // TODO Auto-generated method stub
    19 super.onCreate(savedInstanceState);
    20 setContentView(R.layout.c);
    21 c = findViewById(R.id.c);
    22 c.setBackgroundColor(Color.YELLOW);
    23
    24 button1 = (Button) findViewById(R.id.cButton1);
    25 button1.setOnClickListener(cOnClickListener);
    26
    27 button2 = (Button) findViewById(R.id.cButton2);
    28 button2.setOnClickListener(cOnClickListener);
    29 }
    30
    31 @Override
    32 protected void onPause() {
    33 // TODO Auto-generated method stub
    34 super.onPause();
    35 System.out.println("activity C onPause");
    36 }
    37
    38 @Override
    39 protected void onDestroy() {
    40 // TODO Auto-generated method stub
    41 super.onDestroy();
    42 System.out.println("activity C onDestory");
    43 }
    44
    45 private OnClickListener cOnClickListener = new OnClickListener() {
    46
    47 @Override
    48 public void onClick(View view) {
    49 // TODO Auto-generated method stub
    50 switch(view.getId()) {
    51 case R.id.cButton1:
    52 setResult(RESULT_OK);
    53 finish();
    54 break;
    55
    56 case R.id.cButton2:
    57 finish();
    58 break;
    59
    60 default:
    61 break;
    62 }
    63 }
    64 };
    65 }

    Activity C中有2个Button,一个是关闭自己,另一个是关闭所有的Activity。

    关闭自己很简单,直接finish()就ok了。

    关闭所有的就需要先设置一个返回值,然后关闭自己(注意一点要先设置返回值),这是先走Activity C的onPause(),然后走Activity C的onDestory(),完了之后就会将返回值传给Activity B。

    在Activity B的OnActivityResult(int requestCode, int resultCode, Intent data)中就会对传回来的参数进行判断,如果是RESULT_OK 就先设置Activity A的返回值,然后finish()自己就ok了,这是先走Activity B的onPause(),然后走Activity B的onDestory(),完了之后就会将返回值传给Activity A。

    在Activity A的OnActivityResult(int requestCode, int resultCode, Intent data)中就会对传回来的参数进行判断,如果是RESULT_OK finish()自己就ok了,这是先走Activity A的onPause(),然后走ActivityA 的onDestory(),完了之后就会将返回值传给Activity A。

    点击Activity C上的关闭自己出现的log:

    02-22 06:04:22.107: INFO/System.out(8159): activity A onPause
    02-22 06:04:23.839: INFO/System.out(8159): activity B onPause
    02-22 06:04:25.598: INFO/System.out(8159): activity C onPause
    02-22 06:04:25.879: INFO/System.out(8159): activity C onDestory

    然后点击Activity B上的Button,然后在点击Activity C上的关闭所有出现的log:

    02-22 06:04:22.107: INFO/System.out(8159): activity A onPause
    02-22 06:04:23.839: INFO/System.out(8159): activity B onPause
    02-22 06:04:25.598: INFO/System.out(8159): activity C onPause
    02-22 06:04:25.879: INFO/System.out(8159): activity C onDestory
    02-22 06:06:04.848: INFO/System.out(8159): activity B onPause
    02-22 06:06:06.349: INFO/System.out(8159): activity C onPause
    02-22 06:06:06.879: INFO/System.out(8159): activity C onDestory
    02-22 06:06:06.889: INFO/System.out(8159): activity B onDestory
    02-22 06:06:06.908: INFO/System.out(8159): activity A onDestory

    这只是自己学习中的笔记,留待以后参考。

    代码:

    TestActivityIntent.rar

  • 相关阅读:
    python 使用else代替状态变量
    python 实现线程安全的单例模式
    sql语句的执行顺序
    python 实现int函数
    python实现时间o(1)的最小栈
    python实现简单的负载均衡
    python实现求最长回文子串长度
    python lambda表达式
    sql针对某一字段去重,并且保留其他字段
    基本认识
  • 原文地址:https://www.cnblogs.com/shang53880/p/1961257.html
Copyright © 2011-2022 走看看