I have simple code with transparent activity (it is imposed on my MainActivity). I need to kill this transparent activity by clicking button and after that I need to show dialog. But get some problems first of all if I do that :
public void buttonClick(View view) {
if (view.getId() == R.id.bToK)
{
onDestroy();
}
}
transparent activity it's still visible just nothing happen (in debug mode I see that it goes to onDestroy but I doesn't destroy it at all)
If I change onDestron() to finish() there are other problems because my Main activity became first visible (onResume is called) then called is onDestroy for transparent what provides to next problems during creating this dialog. What should I do If i want show this dialog after kill this transparent ?
Call finish() instead. This will call onDestroy() and respect the activity life. Next if you want open a dialog you can start it before finish or start in the main activity with onResult https://developer.android.com/training/basics/intents/result.html
Related
Normally when the back button is clicked it goes to the previous activity and if the current activity is the first activity, the application closes.
I have a splash screen (that is logically my first activity) and then the menu activity loads.
I want to close the program when the back button is pressed on my menu activity (as if it is the first activity) and avoid going back to the splash screen again, but I know that I should not exit the program.
I was wondering what is the functionality of back button on the first activity?
does it put the program to pause?
Avoid splash screens !
Instead of using your splash as the main activity, use menu activity as the main one, and in onCreate() chain off the splash activity, which will close and disappear forever.
When new Activity is launched from first Activity, first Activity is executed until onStop() method, then it stops and waits for relaunch, unless you killed it by calling .finish(), in that case launched Activity becomes first Activity and back button will minimize application on back button press. In order to control what application does on back button press you can override this method in your Activities and implement your own custom behaviour:
#Override
public void onBackPressed() {
super.onBackPressed()
}
Very good example of how lifecycle of Fragments/Activities work can be seen in picture below:
Hope this helps. Good luck.
While you are on the SplashScreen, on the method calling your menu activity I assume you are doing something like startActivity (new Intent (this, MenuActivity.class)); in Java or startActivity(Intent(this, MenuActivity::class.java)) for Kotlin just after that call finish() this will remove your SplashScreen from the back stack
add this attribute to your splash activity (in manifest):
android:excludeFromRecents="true"
pressing back in per activity, transfer control to prev activity in stack (back stack) that not excluded from "Recents".
I want ask like in title. how to move/send activity to background in android without pause it?
for example in this method:
public void onBackPressed () {
//here what i should wrote to back my previous activity and not stop or pause current
}
That is not possible. By definition:
onPause() will be called when the activity no longer is in the foreground from an input standpoint
onStop() will be called when the activity no longer is in the foreground from a visibility standpoint
What happen if showDialog of activity get called ?
Does activity call onPause if yes why ? As it is the part of Activity only then why onPause gets call?
onPause() isn't called on activity that showed dialog.
onPause() is called when your activity is no longer at the top of the activity stack. A Dialog by itself is not an Activity, so will not replace the current Activity at the top of the stack, so will not cause anything to pause.
A dialog (lower-case) does not need to be implemented by a Dialog class, however. For example it is not uncommon to implement one with an Activity whose theme is set to that of a dialog. In this case displaying the dialog-as-an-Activity will cause the new Activity to be on the top of the stack, pausing what previously was there.
See this answer
A snippet from the Android Activities document(scroll down to the "foreground lifetime" line) says :
An activity can frequently transition in and out of the foreground—for
example, onPause() is called when the device goes to sleep or when a
dialog appears.
I don't quite understand this. Under what circumstances should this happen? Is onPause() called only if the context of the dialog in question is different from the activity on top of which the dialog is to be displayed?
EDIT: Adding code sample to illustrate my doubt in detail
Going by the above-mentioned quote from document, should my activity's onPause() method get called when the AlertDialog (or just the Dialog) in the following code gets displayed? Should I see the "onPause called" log entry when the dialog is displayed?
But I don't see that happen. And it shouldn't either, if I have understood the Android life cycle correctly! So, what's the document pointing at then?
public class LifeCycleTestActivity extends Activity {
private static final String TAG = "LifeCycleTest";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "onClick");
AlertDialog dialog = new AlertDialog.Builder(LifeCycleTestActivity.this).create();
dialog.setMessage("You Clicked on the button");
dialog.setTitle("Dialog!");
dialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.setCancelable(true);
dialog.show();
/*
Dialog dialog = new Dialog(LifeCycleTestActivity.this);
dialog.setTitle("Dialog!");
dialog.setCancelable(true);
dialog.show();
*/
}
});
}
#Override
protected void onPause() {
Log.d(TAG, "onPause() called");
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume() called");
}
}
onPause() is called when your activity is no longer at the top of the activity stack. A Dialog by itself is not an Activity, so will not replace the current Activity at the top of the stack, so will not cause anything to pause.
A dialog (lower-case) does not need to be implemented by a Dialog class, however. For example, it is not uncommon to implement one with an Activity whose theme is set to that of a dialog. In this case, displaying the dialog-as-an-Activity will cause the new Activity to be on the top of the stack, pausing what previously was there.
I've been doing quite a lot of code with dialogs, including the AlertDialog that you mention, and I've also tried to check if onPause() is being called on the activity when the dialog pops up, but thus far my conclusion is that the activity simply keeps running and that onPause() is not called.
Not sure if it helps, but at least you now know that there are others who experience what you're experiencing :-)
Its wrong that activity remains no longer at top of activity stack in onPause phase.
Condition an activity to be onPause state -
Activity partially visible e.g. dialog on activity.
The Activity object is retained in memory, it maintains all state and member information, and remains attached to the window manager.
e.g Home button pressed causes activity to go in onPause(). Still at top of stack.
In fig 1. Activity3 will be destroyed and removed from top stack
In fig 2. Now Task A goes to background but Activty X still on top of stack . If you override onPause() method int this state
Figure 1. A representation of how each new activity in a task adds an item to the back stack. When the user presses the Back button, the current activity is destroyed and the previous activity resumes.
Figure 2. Two tasks: Task B receives user interaction in the foreground, while Task A is in the background, waiting to be resumed.
I think I remember reading in an earlier version of the Android Lifecycle that onPause was called when none of the activity is on display. i.e. if a bit of your activity is still visible under a popup, onPause will not be called.
Maybe some other experts can vouch for this behavior?
In my slightly weird experience onResume gets called with dialog.setCanceledOnTouchOutside(true); but onPause never gets called.
That being said, I think the documentation might focus on system dialogs (e.g. low on battery).
#hackbot
onPause() is called when your activity is no longer at the top of the activity >stack. A Dialog by itself is not an Activity, so will not replace the current >Activity at the top of the stack, so will not cause anything to pause.
everything depends on implementation...
what is a Dialog ? is a Window added to Display by WindowManager///
so the window when it shows is on top of everything .... (Z order)
what is activity... is "thing" that also creates its window....
when a dialog is shown or it's window comes visible on top of an existing activity, then it overrides partial the activity window so existing activity will move to partially invisible state and you will get call to onPause() from ActivityThread.
but to be sure we also need to consider here a one think...
the state of window if is a standalone window shown on top or it is a child window and a parent of it is a activity window....
so when we know
the Window.LayoutParams (FLAGS) we use to add
and what IBinder is used for the Window to show
we will khow how the activity will behave when windows are shown each over other .. as each winndow has a callbacks they are used by activity or dialog to manage their states...
involved components:
android.os.IBinder
android.view.Window
android.view.Window.Callback
android.view.WindowManager
android.view.WindowManager.LayoutParams
android.view.Display
btw:
if you want to know the windows on screen [ applicable only for the process you own - as window belongs to process and those are Sandboxed - each processs is a separate JVM strictly saying "ART" ] you can use a replection see :
android.view.WindowManagerImpl
android.view.WindowManagerGlobal
onPause() is called every Time when an Activity goes background and Dialog or other Activity comes foreGround. This is done to give first priority to something with which the user is interacting. e.g: assume you are in homescreen (which in turn is an activity) of an application, the homescreen is said to be in foreground. and when you go to next screen by pressing some button or a dialog appears the next screen/Activity/Dialog comes to foreGround and homecreen goes to backGround, which just means homeScreen's onPause() method got called.
My application starts with a welcome screen Activity, but that screen has an option to skip that screen altogether in future launches.
What's the proper Android way to do this? Initially, I just automatically detected the skipWelcome preference and switched to the 2nd activity from Welcome. But this had the effect of allowing the user to hit the back button to the welcome screen we promised never to show again.
Right now, in the Welcome activity, I read the preference and call finish() on the current activity:
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
boolean skipWelcome = preferences.getBoolean("skipWelcome", false);
if (skipWelcome) {
this.finish();
}
And then I implement onDestroy to move on to the next Activity:
#Override
public void onDestroy() {
super.onDestroy();
startActivity(new Intent(Welcome.this, StartFoo.class));
}
But this makes for some weird visual transitions. I'm starting to think that I need a base Activity that pops open Welcome only if proper, and then goes to StartFoo.
I can't comment on Mayra's answer or I would (not enough rep), but that's the correct approach.
Hidden in the Android documentation is this important phrase for Activity.startActivityForResult(),
"As a special case, if you call
startActivityForResult() with a
requestCode >= 0 during the initial
onCreate(Bundle
savedInstanceState)/onResume() of your
activity, then your window will not be
displayed until a result is returned
back from the started activity. This
is to avoid visible flickering when
redirecting to another activity."
Another important note is that this call does not block and execution continues, so you need to stop execution of the onCreate by returning
if (skipWelcome) {
// Create intent
// Launch intent with startActivityForResult()
return;
}
The final piece is to call finish immediately in the welcome activity's onActivityResult as Mayra says.
There are a few solutions to this.
Did you try just launching the activity and finishing? I vauguely remember that working, but I could be wrong.
More correctly, in if(skipWelcome) you can start the new activity for result, then when onActivityResult is called, immidiately finish the welcome activity.
Or, you can have your launcher activity not have a view (don't set content), and launch either the welcome activity or StartFoo.