简体   繁体   English

Android:在进行其他活动之前先销毁活动

[英]Android : destroy activity before intent to another activity

I have two activity in my android application : 我的Android应用程序中有两个活动:
1. LoginActivity : User can Login as member by using password and email or as guest 1. LoginActivity:用户可以使用密码和电子邮件以会员身份以访客身份登录
2. MainActivity : Showing user current location on map 2. MainActivity:在地图上显示用户当前位置

Scenario : 场景:
1. User login as guest 1.用户以访客身份登录
2. Go to Main Activity as guest . 2.以访客身份进入主活动。 (No NPE here) (这里没有NPE)
3. User took some action that needed to login 3.用户采取了一些需要登录的操作
4. Intent to LoginActivity 4.意向登录活动
5. I need to destroy MainActivity before directing user to LoginActivity 5.在将用户定向到LoginActivity之前,我需要销毁MainActivity
6. User input username and password on LoginActivity 6.用户在LoginActivity上输入用户名和密码
7. If user exist and password true go to MainActivity 7.如果用户存在且密码为true,请转到MainActivity。
8. NPE (or Null Pointer Exception) happened here. 8.这里发生了NPE(或Null Pointer Exception)。

So, in my case, i needed to destroy MainActivity (Point number 5) before directing user to LoginActivity. 因此,就我而言,在将用户定向到LoginActivity之前,我需要销毁MainActivity(第5点)。 I've tried this : 我已经试过了:

final AlertDialog.Builder builder = new AlertDialog.Builder(context);

final String message = getResources().getString(R.string.NCI);

builder.setMessage(Html.fromHtml("To do your action, we need you to logged in our server"))
.setTitle("LOGIN REQUIRED")
.setPositiveButton("OK",
    new OnClickListener(){
        public void onClick(DialogInterface d, int id){                         
            Intent i = new Intent(MainActivity.this, LoginActivity.class);

            i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(i);
            finish();
        }
    }
);

builder.create().show();

Log Cat 原木猫

07-25 16:04:27.287: E/AndroidRuntime(19823): FATAL EXCEPTION: main
07-25 16:04:27.287: E/AndroidRuntime(19823): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yai.properti.tujuh.tujuh.tujuh/com.yai.properti.tujuh.tujuh.tujuh.MainActivity}: java.lang.NullPointerException
07-25 16:04:27.287: E/AndroidRuntime(19823):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
07-25 16:04:27.287: E/AndroidRuntime(19823):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
07-25 16:04:27.287: E/AndroidRuntime(19823):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
07-25 16:04:27.287: E/AndroidRuntime(19823):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
07-25 16:04:27.287: E/AndroidRuntime(19823):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 16:04:27.287: E/AndroidRuntime(19823):    at android.os.Looper.loop(Looper.java:137)
07-25 16:04:27.287: E/AndroidRuntime(19823):    at android.app.ActivityThread.main(ActivityThread.java:4895)
07-25 16:04:27.287: E/AndroidRuntime(19823):    at java.lang.reflect.Method.invokeNative(Native Method)
07-25 16:04:27.287: E/AndroidRuntime(19823):    at java.lang.reflect.Method.invoke(Method.java:511)
07-25 16:04:27.287: E/AndroidRuntime(19823):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
07-25 16:04:27.287: E/AndroidRuntime(19823):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
07-25 16:04:27.287: E/AndroidRuntime(19823):    at dalvik.system.NativeStart.main(Native Method)
07-25 16:04:27.287: E/AndroidRuntime(19823): Caused by: java.lang.NullPointerException
07-25 16:04:27.287: E/AndroidRuntime(19823):    at com.yai.properti.tujuh.tujuh.tujuh.MainActivity.onCreate(MainActivity.java:817)
07-25 16:04:27.287: E/AndroidRuntime(19823):    at android.app.Activity.performCreate(Activity.java:5163)
07-25 16:04:27.287: E/AndroidRuntime(19823):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
07-25 16:04:27.287: E/AndroidRuntime(19823):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
07-25 16:04:27.287: E/AndroidRuntime(19823):    ... 11 more

I've tried onDestroy() by replacing finish() call method, but NPE (Null Pointer Exception) still happened. 我已经尝试通过替换finish()调用方法来尝试onDestroy(),但是仍然发生NPE(空指针异常)。 As the first time intent to MainActivity, NPE not happended. 作为MainActivity的首次意图,NPE并未发生。

What i want is start MainActivity as the first time . 我想要的is start MainActivity as the first time How i could do that? 我该怎么做?

Many thanks. 非常感谢。

i think there are little conceptual problem Actually FLAG_ACTIVITY_CLEAR_TOP flag is Use is check here: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP so in that case there are no need finish() method it is already Remove mainActivity class but according to document you have to add one more Flag FLAG_ACTIVITY_NEW_TASK to your intent. 我认为几乎没有概念上的问题实际上FLAG_ACTIVITY_CLEAR_TOP标志是Use,请在此处进行检查: http : //developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP,因此在这种情况下,不需要finish()方法已经删除了mainActivity类,但是根据文档,您必须向意图中再添加一个FLAG_ACTIVITY_NEW_TASK标志。

So Replace 所以更换

Intent i = new Intent(MainActivity.this, LoginActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();

with

Intent i = new Intent(MainActivity.this, LoginActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);

thats it... 而已...

每当您需要调用finish()以完成anonymous inner class某些活动finish()如您在上面的代码中所做的那样)时,只需传递完整活动的名称(您要销毁的名称),例如MainActivity.this.finish() 。 (其中MainActivity是您要销毁的那个)

Create a static Activity object which activity finish on other activity and assign activity in this ie you can can add more activities 创建一个静态活动对象,该活动在其他活动上完成并在其中分配活动,即您可以添加更多活动

public class demoActivity extends AppCompatActivity {
    public static Activity self_intent;
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.demo_activity);
            selfintent=this;
    } 

   //Other functions--------------
} 

do same for other activities 对其他活动也一样

on other 在其他

activityCloseBtn= (Button) view.findViewById(R.id.activity_close_btn);
activityCloseBtn.setOnClickListener(new View.OnClickListener() {      
       @Override
       public void onClick(View v) {
          demoActivity.selfintent.finish(); //for finish demoActivityactivity

          //for other activities Activity.selfintent.finish();
          finish();  //for finish current activity
      }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM