I use firebird cloud messaging to receive data in the background, then a dialog (new Activity with Theme.AppCompat.Dialog as style) will appear. My problem is that the surface of the MainActivity is displayed every time in the background. Is it possible to hide MainActivity without terminating it? Someone has perhaps an idea?
When i set in the Manifest at the MainActivity the value
android:noHistory="true"
i looks good, but the notification is then in the app history. this looks bad
I mean when the activity is in Background that the MainActivity is still visible
As you are using an Activity as a dialog, your MainActivity remains in the Activity Stack in a paused state. However, even if you make the Dialog Activity full-screen, it is not guaranteed that MainActivity will be kept alive. The OS can kill the activity anytime if it needs memory for other processes. This is stated on the Activity reference.
Instead you could try one of these alternative solutions:
Use a DialogFragment instead of a new Activity
Use regular Fragments, adding the fragment at runtime, allowing the user to switch between the dialog Fragment and the Main fragment.
Use a FrameLayout, to show/hide the dialog UI inside the same Main Activity layout.
I think are looking for this method
/* When {#link #FLAG_DIM_BEHIND} is set, this is the amount of dimming
* to apply. Range is from 1.0 for completely opaque to 0.0 for no
* dim.
*/
dialog.getWindow().getAttributes().dimAmount = 1f;
Related
When I change the screen size in the multiview / split screen mode, the onCreate function in MainActivity is called again.
Because in onCreate I have a ProcessLifecycleOwner observer:
ProcessLifecycleOwner.get().GetLifecycle().AddObserver(this);
I don't want it to be restarted ... How do I know that onCreate has been called before?
I know you can add:
android:configChanges="screenSize"
in the manifest, but unfortunately needs to "refresh the layout" when resizing.
Android is going to manage the life cycle, and the programmer needs to deal with all eventualities. On this page, there's an abbreviated diagram:
That would indicate that you would need to manage the observer in the onStop().
The basic navigation of my app is a tab bar within one activity in which each tab is its own fragment. I am trying to get the back button to work to go back to the last tab it was on. I can get the back button to work/appear if the activities are going to the first tab by using this in the java class of the activity:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
and this in the AndroidManifest.xml:
<activity android:name=".CreateNewPost" android:parentActivityName=".MainActivity"></activity>
This works fine, however when I go to a new activity from a button within the other tabs, if I use this same method it goes to tab1. How do I tell it to go to a specific tab or the last active fragment?
Any suggestions would help!
When you launch a new activity the old one either paused or stopped , as in either OnPause() or OnStop() method has been called. Then when you navigate back to a previous activity it is either started (OnStart()) or resuemd (OnResume()). And sometime the activity is destroyed and created again when navigating back to it using OnDestroy() and OnCreate() respectively.
The most guaranteed way that I can think of is to store the state of the first activity somewhere persistently and when the user navigates back to it check what has been stored and show that fragment.
You can use SharedPreferences to store this kind of data, see the google developer documentation on that here. Or onSaveInstanceState(Bundle savedInstanceState) check out this SO question on saving activity state. Although from the answers it is clear that onSaveInstanceState should not work in your specific case, but you should look at it any way because it is useful in other cases like changing screen orientation for example.
I have a splashscreen which downloads some files from my webserver.
After downloading I need to start the mainactivity which has to create many buttons at runtime and this needs some time.
Is it possible to start the mainactivity without showing it (so still show the splashscreen) and let the mainactivity its #onCreate() and after it is finished then show the mainactivity?
I think changing setContentView may not work because the buttons need the root layout of the mainactivity.
Unfortunately, NO.
onCreate only call when your activity start.
I suggest you to use only one activity with two fragments ( Splash fragment and Main fragment ).
First, you start activity and show splash fragment. When splash fragment finish, you show the main fragment.
It's faster than 2 activity, I'm sure that.
In my application, I have the Activity and several Fragments (Activity works as Controller and Fragments - as views)
In some of Fragments I need to show AlertDialogs and ProgressDialogs, Activity can change current Fragment.
My problem is: activity can receive broadcasts and C2DM notifications, and when I created AlertDialog, Activity can change fragment, but Dialog stays. So when user clicks on some buttons, app crashes.
DIalogFragments works like a simple Dialog.
Have I dismiss dialog manually or check if fragment is active? Is there any built-in tools?
First of all I might be missing something, no code etc to go by but...
Secondly: maybe you shouldn't be using dialoges? Seems like a cumbersome user interface. Just use fragments for those as well? Though you say you are using DialogFragments so maybe you've already thought about that and use them as "regular" fragments already.
Thirdly: Dismiss the dialogs when the fragment that showed is removed/hidden then? Use the onStop() callback for example in the fragment or a more central place where you are perhaps saving the currently showing fragment and deciding to display a new one.
Dismiss the dialog by calling ´dismiss´ on the Dialog object or Fragment or dismissDialog in the Activity.
See dismissing dialog: http://developer.android.com/guide/topics/ui/dialogs.html#DismissingADialog
You can still call dismiss on those DialogFragments.
http://developer.android.com/reference/android/app/DialogFragment.htm
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.