-1

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.

PS: I cannot post the log cat report as I did not test it on my mac but directly on my phone. I don't have an emulator. I am compiling the code on my terminal and transferring the .apk file on my phone via bluetooth.

EDIT: 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 miraculously 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 Singh
  • 2,343
  • 1
  • 23
  • 42
  • Post the logcat from the crash – codeMagic Dec 02 '14 at 03:09
  • The code you posted is confusing. You state "And my SubDetailsActivity is as follows:", yet the code that follows is "class DetailsActivity..." – EJK Dec 02 '14 at 03:17
  • It's still impossible to say without the logcat output of the crash. What you have posted *seems* fine – codeMagic Dec 02 '14 at 03:58
  • @codeMagic I did not test it on my mac. As I don't have an emulator installed. So I need to transfer the `.apk` to my phone via bluetooth. I cannot post the log cat output due to this reason. – Aditya Singh Dec 02 '14 at 04:10
  • Can you please tell me if this is the proper way to re-start a `finish()`ed activity? If no, then what m I supposed to do? – Aditya Singh Dec 02 '14 at 04:17
  • As I said, it looks fine from what I can see. Install genymotion and/or use Crashlytics, bugsense, or some other logging software. You need to see the stacktrace to see what is causing the crash – codeMagic Dec 02 '14 at 04:22

3 Answers3

2

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.

Harry
  • 422
  • 3
  • 11
  • Hello harry. Actually i want the app to quit once the user presses the back button when on SubDetailsActivity. Coz i want the DetailsActivity to show up when the user launches the app for thw first time. Thats the reason. I am creating a .properties file after pressing the button on DetailsActivity. Next time when the user launches the app, the app check for the file to be present. And if present, the SubDetailsActivity opens up. Else DetailsActivity remains. – Aditya Singh Dec 02 '14 at 12:49
0

Just like Harry answered,

I want to add something for you. If you are programming for higher API Levels (e.g. 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

Community
  • 1
  • 1
Stuci
  • 571
  • 1
  • 5
  • 13
0

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.

Mike
  • 857
  • 1
  • 7
  • 11