Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a call to finish() causing onCreate() to be called, starting a new Activity?

(Yes, I've already looked at existing questions related to this problem.)

I am calling finish() from my Activity's Up button listener. But although onDestroy() does get around to being called, first onPause() is called and then, surprisingly, onCreate(), which is causing real problems. Why is a new ScanningActivity being launched by a call to the finish() method of ScanningActivity?

I am logging invocations of all the life cycle functions and the order is this:

 inside onClick() Listener for up button.
         Inside onPause()
         Inside onCreate()  // this is what's hosing everything
         Inside onStart()
         Inside onResume()
         Inside onWindowFocusChanged()
         Inside onStop()
         Inside onDestroy()

Why am I getting this sequence of events following a call to finish()? Here's the ScanningActivity code that calls finish(), notice, in an onclick listener (which is assigned within the onCreate() method):

@Override
public void onCreate(Bundle savedInstanceState)
{
    . . .

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) 
        {
            Log.i("ScanningActivity", "inside onClick() -- Listener for up button being executed.");

            android.widget.LinearLayout layt = (android.widget.LinearLayout) vCustView;

            View vTemp = layt.findViewById(R.id.scanning_up);
            if (null != vTemp)
            {
                Button btnUp = (Button) vTemp;
                if (!btnUp.getText().equals("End"))  
                {
                    setResult(RESULT_CANCELED);

                    finish();
                    return;
                }
            }
         }
    . . .
}

As per Karakuri's request here is the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.calypso.myandroid">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    //
    <uses-permission android:name="android.permission.SET_DEBUG_APP" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

        <uses-feature android:name="android.hardware.camera" />
        <uses-feature android:name="android.hardware.camera.autofocus" />

        <uses-permission android:name="android.permission.CAMERA" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ail_logo"
        android:label="@string/app_name"
        android:theme="@style/AssistTheme">
        <activity
            android:name=".HomeScreenActivity"
            android:label="@string/title_activity_home_screen"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".GradeActivity"
            android:label="@string/title_activity_grade_assessment"
            android:screenOrientation="portrait"
            android:parentActivityName=".HomeScreenActivity"
            android:noHistory="true"
            />
        <activity
            android:name=".ScanningActivity"
            android:label="@string/title_activity_scanning"
            android:screenOrientation="portrait"
            android:parentActivityName=".GradeActivity"
            />
    </application>


</manifest>

... and here is the calling code, which initially launches ScanningActivity from inside the GradeActivity:

if (bFinishedSuccessfully)
{
    try
    {
        Log.i("GradeActivity", "SOMEHOW THIS IS AGAIN STARTING THE ScanningActivity!!!");

        Intent intent = new Intent(this, ScanningActivity.class);
        intent.putExtra("SessionInfo", m_hmActivate);
        startActivity(intent);
    }
    catch (Exception ex)
    {
        Log.i("GradeActivity", "ex: " + ex.getMessage());
    }
}

UPDATE: I modified the manifest a bit and the calling code, slightly, in an attempt to fix this, but to no avail. The behavior is still exactly as described originally. I've updated the question with the mods.

like image 231
Alyoshak Avatar asked Dec 02 '16 02:12

Alyoshak


People also ask

What will happend when you call finish () in onCreate?

As per official documentation: You can call finish() from within this function, in which case onDestroy() will be immediately called after onCreate(Bundle) without any of the rest of the activity lifecycle (onStart(), onResume(), onPause(), etc) executing.

What finish () function does?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

Why do we need to call setContentView () in onCreate () of activity class?

As onCreate() of an Activity is called only once, this is the point where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById to programmatically interact with widgets in the UI, calling managedQuery(android.

Is onCreate only called once?

OnCreate is only called once.


1 Answers

I think that the problem is on your manifest,the android:noHistory="true" parameters added to GradeActivity. The setResult() on ScanningActivity will try to create the parent Activity GradeActivity. I think that if you don't need to keep GradeActivity on your navigation stack, you have to start the ScanningActivity from your HomeScreenActivity using startActivityForResult.

like image 193
Anis BEN NSIR Avatar answered Nov 15 '22 20:11

Anis BEN NSIR