简体   繁体   中英

Issues re-starting a finish()ed android activity

I have two android activities with one single button on both.

The activity DetailsActivity is my MAIN activity.

public class DetailsActivity extends Activity {

    @Override
    public void onCreate(Bundle bundle) {

        super.onCreate(bundle);
        setContentView(R.layout.details_activity_layout);
    }

    @Override
    public void onResume() {

        super.onResume();
    }



    public void gotoSubDetails(View view) {

        Intent intent = new Intent(this, SubDetailsActivity.class);
        startActivity(intent);
        finish();
    }
} 

And my SubDetailsActivity is as follows:

public class SubDetailsActivity extends Activity {

    @Override
    public void onCreate(Bundle bundle) {

        super.onCreate(bundle);
        setContentView(R.layout.sub_details_activity_layout);
    }

    @Override
    public void onResume() {

        super.onResume();
    }



    public void gotoDetails(View view) {

        Intent intent = new Intent(this, DetailsActivity.class); 
        startActivity(intent); // Restarting the finish()ed activity here.
        finish();
    }
} 

And this is how I mentioned my button in details_activity_layout.xml for DetailsActivity.java :

    <Button android:id="@+id/details_submit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/to_sub_details"
        android:onClick="gotoSubDetails" />    

And this is how I have mentioned my button in sub_details_activity_layout.xml for SubDetailsActivity.java :

    <Button android:id="@+id/sub_details_submit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/back_to_details"
        android:onClick="gotoDetails" />    

These are my both activities in AndroidManifest.xml :

    <activity android:name="DetailsActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name="SubDetailsActivity">
    </activity>

When I click the button in DetailsActivity , the activity finish() es properly and SubDetailsActivity starts up.

But when I click the button in SubDetailsActivity to get back to the finish() ed DetailsActivity , the app straight away crashes on my LG L90 phone.

Where am I going wrong? Any help? Please.

I cannot post the log cat report as I did not test it on my mac but directly on my phone. 我无法发布log cat报告,因为我没有在Mac上进行测试,而是直接在手机上进行了测试。 I don't have an emulator. I am compiling the code on my terminal and transferring the .apk file on my phone via bluetooth.

Ok guys. 好的。 Now this is really funny!

I removed the

android:onClick="gotoSubDetails"  

and

android:onClick="gotoDetails"  

from my details_activity_layout.xml and sub_details_activity_layout.xml respectively. And I loaded the android.widget.Button 's in my DetailsActivity.java and SubDetailsActivity.java and added an android.view.View.OnClickListener on both of them and overrode the onClick(View) method.

This is what I did in my DetailsActivity.java :

public class DetailsActivity extends Activity {

    @Override
    public void onCreate(Bundle bundle) {

        super.onCreate(bundle);
        setContentView(R.layout.details_activity_layout);

        Button button = (Button)findViewById(R.id.details_submit);
        button.setOnClickListener(new DetailsSubmitListener());
    }

    @Override
    public void onResume() {

        super.onResume();
    }


    public class DetailsSubmitListener implements OnClickListener {

        @Override
        public void onClick(View view) {

            Intent intent = new Intent(this, SubDetailsActivity.class);
            startActivity(intent);
            finish();
        }
    }
}  

And this is what I did in my SubDetailsActivity.java class:

public class SubDetailsActivity extends Activity {

    @Override
    public void onCreate(Bundle bundle) {

        super.onCreate(bundle);
        setContentView(R.layout.details_activity_layout);

        Button button = (Button)findViewById(R.id.sub_details_submit);
        button.setOnClickListener(new SubDetailsSubmitListener());
    }

    @Override
    public void onResume() {

        super.onResume();
    }


    public class SubDetailsSubmitListener implements OnClickListener {

        @Override
        public void onClick(View view) {

            Intent intent = new Intent(this, DetailsActivity.class);
            startActivity(intent);
            finish();
        }
    }
}  

And this worked fine. 很好。

So what was the problem with the .xml attributes in my layout files? And how different is it from the hardcoded listener in my .java file? This thing has confused me. finish() ed activities can be re-started. Thats what I discovered with my change in the program. Please shed some light of knowledge on this.

Aditya,

If you want to traverse between 2 activities like in your case, you should never finish Details activity. Instead without finishing Details activity, start SubDetails activity. If you want to come back, no coding required. If user presses BACK button, SubDetails Activity will be finished and DetailsActivity will come to foreground.

Finishing one activity and starting it again, is unnecessary overhead. Avoid it. Finish activity if it is absolutely necessary.

Also, there is no any way to restart finished activity.

Just like Harry answered,

I want to add something for you. If you are programming for higher API Levels (eg Android API 13+) you can define a parent activity.

This way you don't need to make such complex implementations.

For reference

Google Developers Guide to UP navigation

Stackoverflow ANSWER on the topic

I think you should read this!:

http://developer.android.com/guide/components/tasks-and-back-stack.html

I think it's very important how you declared the launchMode in your andriodManifest for your activities ! There should be the problem. Read carefully what is a task and how a back stack in android works.

Maybe you are finishing the task but at same time you are viewing the next activity which is in the same task ! After reopen it crashed because of this I think. Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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