I have an activity, DogActivity, with a slider. When the user slides view PawsView to a certain degree, I start another activity, CatActivity, using startActivity(intent). If the user clicks the back button, normally the user returns to DogActivity. Here is my problem: if in Developer options I set Do not keep activities then when the user clicks the back button and thus returns to DogActivity, the slider is not asserted and so PawsView is back to its original position; however, if I don't have that option selected, upon returning to DogActivity the slider is still asserted (the sliding already occurred).
Since I don't want to depend on the user selecting or deselecting Do not keep activities, I need to do this programmatically. So does anyone know how to do this? I have tried putting the appropriate code inside onResume but that has no effect. It's as if finishing CatActivity has no effect on DogActivity. BTW, the view I am using to display PawsView is a custom view.
I already tried using a handler with postDelayed to pull PawsView back to normal, but the handler always executes before the startActivity is executed. If on the other hand I start a proper Thread to run the call to close the slider, I get an error about the wrong thread trying to change the layout.
Another way of asking the question may be: How do I force onResume to be called even when Do not keep activities is NOT selected on a user's device.
You could try to launch CatActivity using startActivityForResult and then handle the result in onActivityResult and do the necessary setup from there. It's sort of like forcing onResume.
Related
I have a widget that launches a dialog with two options. One Button to make emergency call, and another to call customer service. Once the dialog is launched from a widget, and I tap on one of the two option, the button doesn't respond. But, if I background the app and bring it to foreground, then that previous selection of the button that I made gets called. I'm using the correct flag when launching the activity from the Widget.
The order of the lifecycle of the Fragment that takes place when things are working normally with the Dialog is below. The similar lifecycle takes place when foreground and backgrounding the app. Not exactly sure why the callback for the buttons on the Dialog doesn't respond when launched from the Widget. Thank you!
OnCreateView()
OnViewCreated()
OnStart()
OnResume()
I was able to fix this problem by simply using navController.navigate(). This allows the NavController to handle the Fragment lifecycle properly and in the correct order.
I'm creating an app where I display a list of pending challenges. When the user clicks on a challenge, he can accept it or ignore it.
Here's what I want to do and I don't know how :
if the user accepts or ignore the challenge, call this.finished and remove the challenge from the list
if the back button is pressed, do nothing, the challenge is still visible
In short, if the user really responds to the challenge I don't want it to be displayed in the list, but if he doesn't choose any option and press the back button, he didn't choses one of the two actions so I want that challenge to still be visible in the list.
I don't think it's possible to detect what button I've pressed when i go back to my main activity. I've thought about using global variables, but I don't want to misuse them either.
Just to be clear, I'm not asking how deleting a list item. But when to know deleting one depending of the actions of another activity.
Give your second activity the index you want to remove as a parameter inside the intent and let it finish by returning the index again as an intent extra (by using setresult(Intent i) and then calling finish) inside your first activity catch the result from your second activity by overwriting onActivityResult (http://developer.android.com/reference/android/app/Activity.html#onActivityResult(int, int, android.content.Intent))
see 3.3. Retrieving result data from a sub-activity in http://www.vogella.com/tutorials/AndroidIntent/article.html for a detailed howTo
As Android newbie I started to wonder about the Activity lifecycle. I'm having an Activity that loads a list of Persons and displays them. Upon the click of a Person I want to open another Activity showing the details of that Person. I'm currently doing this by creating an Intent on the "PersonDetailActivity" which I then start. So far so good. On the PersonDetail page I would like to have a menu action to go back to the Person list. I again applied the same technique, meaning an Intent that brings me back to the PersonListActivity.
Now I started to wonder what returning to the PersonListActivity means. Will a new instance get created and will I have to reload the persons that it displays in the list? Can you come back to the same instance, avoiding having to reload the list again? Do you then have to pass a pointer to yourself via the intent to the other Activity?
So when will and Activity be re-instantiated and when will it not. Any hints or suggestions are more than welcome. Maybe there are some patterns to be applied for these back and forth menu actions that I'm not yet aware of.
Thanks,
Vincent
Yes,,. Call finish() in second Activity instead of starting new Activity..
There is basically something called Activity stack which stores all Activities in the order they were started.. so if start new Actvity , that sits on top of the stack and preveous one gets below it.. when you call finish the Activity is poped out..
if you don't want to call finish() correct waht ever you were doing then add flag ACTIVITY_CLEAR_TOP in manifest for the 1st Activity..
Basically if you just call the finish() method on your PersonDetailActivity
PersonDetailActivity.this.finish();
it will activate the onResume() method from the Activity that is on the top of the finished one, which here would be your PersonsActivity. You can specify in your onResume() method what you want to perform when turning back there.
Here i have a few activities that consist different menus in my app..
The problem is that i want to add a are you sure popup box to exit the current menu and return back but calling finish() method on the click event of yes button of popup box causes all activities to terminate and app exits...
I want to make a way to terminate only the foreground activity and
return to last activity programatically (i.e without using back key)
Can u post some source code regarding how you start you new activities? Are you starting multiple activities at all? finish() method only finishes the current activity and not the entire stack of activities, thus the system automatically brings to front the previous activity from the stack. I can't understand your question please provide some further details.
i have the following question.
I have an activity which is showing a progress bar while a service is downloading data from an API.
I want that when i press Home and relaunch my program the activity and which is not the first activity called but it is in the stack was recovered in order to continue showing the progress.
I have read about the cycle of life of activities but i don't find a clear solution.
The same thing happens If i am in the activity which is showing the progress bar and i press a button that takes me to another activity, when i go back, can i recover the old activity instead launch one new?
Anyone helps me?
Thanks
The problem is that pressing the home button will erase the whole activity stack. That means there is no possibility to go back to the activity it even is not certain that the activity still exists.
If this a progress that is interesting for the user that it is still running you could display a notification bar icon until the progress is finished. I think you can specify a special intent for clicking on the notification bar and filter this intent with your activity. That way you would go back to the activity. But you still face the problem that the activity is saved and has no reference to the background thread doing the work.
If your Activity has left the stack its finish method is called. You shouldn't try to reuse this activity later on. The best way is to think of a way that the whole state of the activity can be saved and restored later on. To restore a reference to the background thread doing the work you could subclass the application class and save a reference to the running task in your subclass.