0

My app has 4 different screens and I am trying to set up a tab-like structure, only using buttons. For simplicity I'll just call them A,B,C,D.

Right now in the onCreate method of each Activity, I have 3 button Button listeners (in Activity A, I have listeners for Button B, C, D) that trigger a startActivity when the respective button is pressed.

The problem occurs when I navigate from say A->B and then try to go back B->A using the buttons, my application crashes. I believe this is because A is paused in the background, and trying to start a new instance of it makes the program crash.

So I was wondering if there is a way to resume Activity A without creating a new instance of it? I was expecting there to be some sort of resumeActivity() Method, but it doesnt seem to exist.

Thanks for any help.

5 Answers5

0

Try this:

public void onResume() {
    super.onResume();
    // do your stuff here
}
Eivind
  • 123
  • 7
  • So when I call startActivity() and the activity has already started, and is paused, it will go to the onResume() instead of onCreate()? – user1109893 Dec 21 '11 at 13:25
0

You have many ways to do this.

1) Set LaunchMode of Your Activity A to SingleInstance

2) Dont call Activity A with Intent, Just Finish B, It will Automatically show A.

3) See this answer and make only one base class and extend it elsewhere in A to D and write all button click in Base Class only.(To Simplify your code)

4) If necessary Override onResume() of Activity A.

Community
  • 1
  • 1
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
0

You can also use the TabHost if you don't want to make the entire functionality yourself.
Here's a tutorial: http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

Jave
  • 31,598
  • 14
  • 77
  • 90
0

Use a TabActivity
And then if you don't want to use the default implementation of the tabs, then I think you would remove the <TabWidget> from the xml and use TabHost.setCurrentTab to change which Activity you are on.

Reed
  • 14,703
  • 8
  • 66
  • 110
0

You should consider using Fragments instead. Having A, B, C and D be a Fragment each and then have one single Activity that only handles the navigation between the Fragments.

kaspermoerch
  • 16,127
  • 4
  • 44
  • 67