I am trying to add a save dialog to an Activity.
If you press back, I already have captured it and added a save dialog box.
As I have a generic action bar, is there a way to capture the change of activity event without hardcoding it into the activity change itself?
I Googled it a bit and found no luck, onDestroy and onStop don't seem to do what I want.
If you want to know whenever the Activity ceases to become visible (for whatever reason), override onPause (see http://developer.android.com/reference/android/app/Activity.html).
As an aside, as a user, I would want this behaviour - if I move away from an Activity without pressing the back button or quit, I am hoping that it will handle everything silently - ie you use onPause (or alternatives) to store things so that when the activity resumes it has everything as I left it.
One way you could do this would be write a class which inherits from Activity and implement the behavior in that class. Then simply inherit this class for all your activities.
You could write one class which implements the save behaviour
class SaveActivity extends Activity {
//...
#Override
public void onBackPressed() {
// common behaviour you want
}
}
Then in your activities you could do this
class MyActivity extends SaveActivity {
// code for this activity
}
onPause() might be what you should be looking at. It is called when any activity comes onto the top of this Activity.
Related
I'm like an intermediate in android programming. I decided to take a deep dive into the Activity lifecycle methods and I realised something, like why do methods Toasts.show() and many other methods get called again when the activity is resumed. If the methods are in the onCreate so why then if you like go to another activity and return it will still give you a Toast message. Let me give an example.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
{Your Initialization go here }
//Toas message
Toast.makeText(code).show();
}
}
So imagine you leaving this activity for another one and then coming back... why does it still show the Toast message. Because since the lifecycle is
OnCreate
onResume
onStart
onPause
onstop
onDestroy
And when you come back to your MainActivity it calls the
onResume
onStart.
So if onCreate is not called...So how does the Toast message show.
Please someone should help me answer this I've searched all day but couldn't find answers.
accoording to lifecycle of activity https://developer.android.com/guide/components/activities/activity-lifecycle, if there is no memory engouh the app process will kill, so when you back to your activity on created will call another time.
the image below for clarification:
When your activity goes in the background, and memory is required for other apps. It is possible that the system frees up space even though your activity is in the background with ONPAUSED state. and now if you navigate to your activity, since the saved instance is removed due to memory requirements. instead of ONRESUME, ONSTART is called. you can read more about it here: https://developer.android.com/guide/components/activities/activity-lifecycle
and see this image for better understanding: https://developer.android.com/guide/components/images/activity_lifecycle.png
This uncertainty is one of the reasons why we now mostly use view models for a sizeable app.
I have two activtiy. When a boolean variable in activity 1 is true then the UI in activity 2 should be update (activity 2 is with fragment). When I back to activity 2 with (setDisplayHomeAsUpEnabled) it works correctly but with back button it does not work. What is the difference between this ways and how I can solve this. I try update the activity 2 with this code but it does not work:
#Override
public void onBackPressed() {
super.onBackPressed();
//update();
}
The Home Button is not meant to behave like the Back Button. See this reference. The Home or "Up" Button should take the user up within the view hierarchy (i.e. if you have a number of multiple choice fragments, don't go back through each of them, but back to the activity that started the first fragment)
Each activity should specify what it's parent activity is in order to properly use this functionality. You can do this through the manifest, or by overriding onOptionsItemSelected() as outlined here
In terms of why your activity 2 may be updating when using the "Up" button but not the back button, this could be because both handle the back stack in a different way. Back will take you to the last thing on the stack and resume it, assuming you don't haven't tagged the activity with a launchmode that alters this behavior. Here's more info on the back stack. If you haven't designated your Activity 2 as "singletop" then when you use the "Up" Button, a new instance of that parent activity is created. See here. This may means that the information is updating after using the home button because it's creating a new instance of the activity, while for the back button - you are not creating a new instance but resuming a previous instance... make sure you implement an onResume() function to properly handle this and update the information.
In my activity I add to it some stuff by checking checkbox and if
list.size()>0 shows up button which is redirecting me to second activity. In second activity I display list, when I click on it i delete object from list. Ive made button in second activity which make this list.clear(); finish(); When I return to first activity i've still visible button. How to solve it ?
you must read your 'list' at activity onResume() method: when you return to your activity, onStart() is not called since it was not killed by the system
see the entire cycle in this link: http://developer.android.com/training/basics/activity-lifecycle/stopping.html
I would use Gson to pass by intent any object of the class that has already been defined (not by you) and is not Serializable. Just for convenience, if you would like to pass an object of some class created by you, the class should extend Serializable
I receive an exception when trying to show a DialogFragment from within the onLoadFinished method of a Fragment that implements the LoaderCallbacks interface. Basically I am using the LoaderCallbacks to get some data from a rest service and then on the onLoadFinished I am trying to show a custom DialogFragment that contains a ListVeiw to allow the user to make a selection. Everything works great except when I try to launch the dialog from within the onLoadFinished. How can I accomplish this..and is this the correct approach to the problem.
Here is an example of what I am trying to do:
public class EventFragment extends Fragment implements LoaderCallbacks<someresponse> {
#Override
public void onLoadFinished(Loader<someresponse> arg0, someresponse data) {
//an exception is generated when trying to launch a dialog fragment from
//within the onLoadFinished
FragmentManager manager = getFragmentManager();
ListViewDialogFragment dialog = ListViewDialogFragment.newInstance(data);
dialog.show(manager, "event_list_dialog");
}
}
Thanks!
So after some research, I have determined that my approach was incorrect. You should never try to launch a new fragment from within the onLoadFinished method of an LoaderCallbacks async task. The framework tries to keep you from doing this because the state of the currently running fragment or activity, that implements the LoaderCallbacks is indeterminate and therefore not guaranteed to be there when the async task finishes.
Additionally trying to separate a processing dialog state and data display state into two separate fragments is a bit counter to the MVC design pattern that the android framework supports. That said, my new approach consisted of dynamically changing the view for the fragment implenting LoaderCallbacks to hide or show a specific linear layout, one for the process indicator and one for the display of the data. This approach left me modifying an existing fragment instead of launching a new one which worked out great.
Here is a link to the discussion that finally gave me clarity.
https://groups.google.com/forum/#!topic/android-developers/dXZZjhRjkMk/discussion
The approach you define is quite good, except one case - what will happen if your activity had already gone from the screen, when loading operation got finished? How it will show dialog in this case?
So generally, I would appreciate if you'll tell which exactly exception did you get.
However, as a general approach, it can be useful to check is the activity holding the fragment is still on top or even was it finished or not.
Even better - you should consider cancelling all background operations when activity/fragment is destroying, in this case you'll have no problems with showing dialogs.
Good luck!
My application shows an alert that the user must respond to before continuing to do other things. I'm trying to figure out the best way to implement this. Using an Activity for the alert isn't quite working.
In my current implementation, the alert is activity (A). When another activity from the same package is started and onStop is called, it starts itself again using FLAG_ACTIVITY_REORDER_TO_FRONT so that it's always at the top of the stack. This works as described, unless Activity A uses Theme.Dialog or Theme.Translucent.
Modified log:
Activity A created
Activity A started
Activity A resumed
Activity A paused
Activity B created
Activity B started
Activity B resumed
Activity B gains window focus
Activity A stopped
Top activity in stack is Activity B, so Activity A relaunches itself
Activity B paused
Activity A started
Activity A resumed
The top activity in the stack should be Activity A, however Activity B remains in the foreground.
Another implementation detail: my application is not for a phone, so I'm not concerned with a back button finishing the activity or interactions with other apps. Still, I agree that on principle I should prevent such problems anyway, so in my code I check whether the activity that has come in front is from the same package (i.e. from our code base). This should work around the theoretical problem of interfering with other apps.
Is there a way to bring Activity A into focus? I understand that this is unusual behavior, but it is necessary for Activity A to remain in the foreground until it is deliberately finished.
I'm also open to suggestions about a completely different and better approach!
FWIW, I'm running 2.2.
(Cross-posted from http://groups.google.com/group/android-developers/browse_thread/thread/d46fd7d59abe15a0, where we got no response.)
You can't do this. Please don't do this. The activity at the top of the stack is the one that has input focus. What you are trying to do fundamentally breaks the user interaction that is supposed to happen.
What you are doing is generally considered by the platform to be an abuse of it, and Android has increasingly been doing things to prevent applications like this from causing harm.
Well, here's what I had in mind:
public class ActivityA extends Activity
{
...
public void onStop() {
super.onStop();
finish();
Intent i = new Intent();
i.setClass(getApplicationContext(), ActivityA.class);
startActivity(i);
}
}
ActivityA is finished in onStop() and started again right away. You might have to check issues regarding device rotation, but this approach should work.
Having window focus means that activity B is still in its visible lifetime, since it has on top the activity A which has a translucent bg or is dialog-like.
Having the window focus doesn't mean that activity B is on the foreground. They are different things.
If you don't want this, then don't use those two themes.