Android back button does not restart activity? - android

My app intiates an activity. On the click of a button, the app opens up the browser with a webpage. When I hit the back button, it comes back to my initial activity screen, but does not resume or restart the activity.
When I put all the layout code and activity code in onResume instead of onCreate, the activity gets restarted.
My question is whether this is the right way to go about it? Can I use onResume to draw my layout and initiate the activity, or is this poor design? When the browser fires up, does the initial activity forget its layout?
Please let me know what you suggest.
Thanks
Chris

Mostly you should read about the Activity Life Cycle.
It is fine to initialize in onResume as long as you only do it once. Either have a dedicated hasInitialized member or check some other value that will have equivalent meaning, and do not initialize again if it is set.

Related

Showing view from onPause of an android activity

I need to show one of my view when onPause is called from an Android activity.
Would it be a good practice to do? Is there any issues if I show it from the onPause?
This view will be shown as a background when my activity is partially visible.
Basically it is not good practice to do so.
The Activity is still slightly visible (link), but the user may not even notice if you change anything.
Also everything in onPause should be fast.
The documentation says:
When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here.
So it will slow down the start of the next Activity, also the Activity animation might be a little laggy if you are updating the UI in onPause()
You may use onResume() to show your view.

Where to put preference based layout operations in ActionBarActivity

In my app I am creating a custom preference screen. This screen in reached via mainActivity (launcher). The mainActivity also shows the current status of some of these settings by reading them and laying them on views. Now, when the user will reach one of the preference screens and edit them, by design I aim to bring him back to the mainActivity (it would be intuitive to press the back button). This time however I want to show the latest edited settings.
I am used to putting layout operations in onCreate. But, in this case onCreate will only be called once when the activity starts and read the status of preference settings and lay them on screen. However, when he will open a settings activity, edit them and press back button he will not see his latest settings laid on screen as onCreate need not be called.
So, on what activity callback should I place the operations to read preferences and lay them on views.
This is based on my understanding and I may have messed up big time. Guide me, Thanks...
To anyone looking for an answer. Its pretty basic stuff. Create function in your main activity to read preferences and update your views. Now, start your preference activity using startActivityforResult() and when result arrives from that activity, run the same function inside onActivityResult(). Done.

Launch the Android App which is in No Longer Visible State

My issue is when an Android Application goes in the background. When I click the Home button and launch my app again from the home screen by clicking the Application icon, it should display the the same screen from which I went to the Home screen. But it calls the onDestory() method then comes out of my application. I thought the application is killed by the system because of memory requirement etc., but I need tokeep the activity and it should again show the same screen where I left instead of starting all over again.
This may be achieved like maintaining sessions.
try putting
android:alwaysRetainTaskState="true"
in the androidmanifest.xml for those activities, i think ICS does that by default now.
The Application will show you the same screen when return after "home" button if your app with different screens is made of different activities to show the interface parts... But if you just make some objects
visible=true or false
so, after resume you'll see the first view... Try to use Intents between different activities... And show a piece of code to help you... Maybe the problem is in overriding of onDestroy, onPause, onResume methods

Back to previous activity

I have a back button that will work exactly like that.
I know using back button is not a good android practice since android device have one. But my situation badly need it.
I cannot finish my activity and its tough to Keep track of the activity stack.
Since you know that overriding BACK button function is not a good practice, just follow it. Tracking activity stack and promising the BACK behavior can be solved by properly setting the launch mode of each Activity. (please see: Launch Mode in Android Official Site)
If you insist to do something like pressing the BACK button, it's super.onBackPressed().
its better for you to user super.onBackPressed()
Refer Go back to previous activity
I wonder why you wouldn't want to finish your activity. Could you move whatever it is in your activity you don't want to destroy into another object?
BTW: the activity will also be destroyed and re created if the screen is rotated.

Make new activity take the place of the parent in the stack

I have read about activities, stacks and launch modes, but have a hard time understanding how to apply this info to my specific problem.
Basically what I want to do is to launch a new activity, and make sure that it takes the parent's place in the stack, rather than being placed on top of it in the stack. I have the following two scenarios:
I have a login-activity. When the user logs in, a new activity is launched. When the user hits "back", I don't want him to be sent back to the login-activity, but rather to exit the program.
I have an activity that is displayed in a tabhost (or rather in a framelayout inside a tabhost, I suppose). This activity has a button, an clicking on this launches a new activity. I would like this new activity to take the parents place. That is, i don't want to open a new full-screen activity, but instead I want it to take the parent's place inside the framelayout in the tabhost. Also, I don't want a press on the back-key to lead the user back to the parent activity.
I would appreciate it if anyone could point me in the right direction. Thanks in advance.
All you need to do is call finish() in your Login Activity. That will remove it from the Activity Stack.
This is a more difficult answer. My best suggestion here would be to either use a ViewFlipper and instead of starting a new Activity you can just manage switching the views out. Alternately, if you need the button to start a new Activity, you could use an ActivityGroup and manage the Activitys that way. They both have their own complexities, so you'll need to investigate which may work better for you.
In the first case, you need to call the finish() in your login activity.
Also check this manifest settings to finish the activity when a new task is launched.
http://developer.android.com/guide/topics/manifest/activity-element.html#finish
In the second case you may use two views as frames or a ViewFlipper. Then overwrite the keyevent to handle the back button click.
http://developer.android.com/reference/android/view/View.html#onKeyDown(int,android.view.KeyEvent)
Check if the user clicked when they are in the second view and show your first view. And if they are in the first view you may want to return false on the method that you overwrite to let the system to handle the event. [Probably close that activity]
Alternatively for the second option, it might be possible in some cases to just remove tabs and use the options menu. This way you can use your activities in a normal way.

Categories

Resources