Search code examples
javaandroidandroid-intentandroid-activitystart-activity

Can't get back to main activity in Android studio


In my project I have two activities: MainActivity.class and SecondActivity.class.

To switch from the MainActivity to the SecondActivity I use the following code:

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

And it works.

I use the same code to switch from the SecondActivity to the MainActivity but the app crashes:

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

If I try to open the MainActivity from the MainActivity itself it crushes too, but this doesn't happen if I try to open the SecondActivity from the SecondActivity.

Any idea?

Here my stack trace:

2021-09-29 17:25:56.843 25827-25827/st.com.st25androiddemoapp E/AndroidRuntime: FATAL EXCEPTION: main Process: st.com.st25androiddemoapp, PID: 25827 java.lang.RuntimeException: Unable to resume activity {st.com.st25androiddemoapp/st.com.st25androiddemoapp.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4270) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4302) at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:52) at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2044) at android.os.Handler.dispatchMessage(Handler.java:107) at android.os.Looper.loop(Looper.java:224) at android.app.ActivityThread.main(ActivityThread.java:7562) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference at st.com.st25androiddemoapp.MainActivity.onResume(MainActivity.java:279) at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1454) at android.app.Activity.performResume(Activity.java:8050) at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4260)

Thank You


Solution

  • When you start MainActivity using startActivity(intent), Intent.getAction() method will return null. This method is called in MainActivity#onResume.

    To prevent your app from crashing, you need to make sure that action is not null before calling any methods (equals() in your case.)

    I found the source code. Just and action != null && here

        if (action != null && (action.equals(NfcAdapter.ACTION_NDEF_DISCOVERED) ||
            action.equals(NfcAdapter.ACTION_TECH_DISCOVERED) ||
            action.equals(NfcAdapter.ACTION_TAG_DISCOVERED))) {
    
            // If the resume was triggered by an NFC event, it will contain an EXTRA_TAG providing
            // the handle of the NFC Tag
            Tag androidTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            if (androidTag != null) {
                // This action will be done in an Asynchronous task.
                // onTagDiscoveryCompleted() of current activity will be called when the discovery is completed.
                new TagDiscovery(this).execute(androidTag);
            }
        }
    

    don't forget to add brackets ()

    It will fix the NPE, but I'm not sure if this is the best solution. I don't know what you are trying to achieve.

    upd If you want to navigate back to main activity by closing Second activity, which was opened on top of MainActivity, then just use finish() method