Modal activity in android , prevent obscuring focused activity - android

I have an application that is based on windows principles , and doesn't not follows the android way . . .
Do not ask why , it is just an requirement, I know that should not be done that way....
Here is the problem:
I have an application that fires two transparent activities , activity A and activity B.
if activity A is shown than if the activity B should be shown the is no problem, because the activity B will be over the activity A.
But if the transparent activity B is shown then if I fire an intent the activity A will be over the activity B ,.. :(, the business logic is that if B is shown then A should be also shown but behind the A. Then is the user dismiss the B , only then the A will be visible...
I hope I describe this good, I far as I know this is not possible, but I want to ask more experienced people
if there is possibility to start some activity that will not be covered by others activities
NOTE: I am talking for preventing activities only from my application

Make A search for a special command keyword in its Intent extras, If it is found, A also launches B from its onCreate(). So, to launch A, use simple intent of A; to launch B, put the special keyword in A's intent, A will launch and also launch B.

Related

Android Notification click keep stack and bring app to front if need. Pass data if specific activity is on top

How can I click a notification, bring my app to the foreground if needed, and keep the stack as it was (even when the app went to the background)?
I have a NotificationClickActivity that is launched when the user clicks a notification.
When the notification is clicked, I have two possible scenarios:
User is logged out from the app
User is logged in.
In the first scenario, NotificationClickActivity starts the login process and receives the result. If OK, I launch MainActivity. The task is cleared so MainActivity is the only activity that I have in the task. I have no problems in this scenario.
In the second scenario NotificationClickActivity does this:
finish()
startActivity(MainActivity.createIntent(this).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP).putExtra("notification", notification))
With the above code, if the app is already running (MainActivity is the root and top activity) and if on the background or in the foreground, I have no problems. MainActivity onNewIntent is called and I can do whatever I what with the notification. In this case, I dispatch the notification to a repository.
The problem I'm facing is when I have activities on top of MainActivity. Imagine the following stack:
MainActivity -> ActivityOne -> ActivityTwo -> ActivityN
With the code that I currently have if I click the notification the stack becomes:
MainActivity -> ActivityOne -> ActivityTwo -> ActivityN -> MainActivity
I want the stack to stay MainActivity -> ActivityOne -> ActivityTwo -> ActivityN but still pass the notification to MainActivity so it can be dispatched to the repository.
Any idea how can I achieve this?
I've been playing with the intent flags for some time but without success.
Of course, I what this without any visible animations or whatever so that the user does not realize what is going on.
Think you're looking for FLAG_ACTIVITY_REORDER_TO_FRONT
.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP || FLAG_ACTIVITY_REORDER_TO_FRONT)
This will bring the activity to the top of the stack if its already running.
consider a task consisting of four activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then B will be brought to the front of the history stack, with this resulting order: A, C, D, B.
In the above example, if you want to go from A,B,C,D to A,B,C,D,B instead, then you need to make sure your activity supports multiple instances.
Suggest FLAG_ACTIVITY_SINGLE_TOP to be specified in android manifest for the activity instead. Then in your onNewIntent you should check if the user is logged in. If the user is not, you should launch the login process with startActivityForResult and handle the login result with onActivityResult.
This assumes your login can be initiated with an intent, which calls setResult with enough information for the caller to determine if the login was successful.
As for animations, I think you will have to use fragments and disable animations, or disable animations for the activity, and do custom transitions only when you want it. This may even not work with activities, since the OS/OEM defined transitions may just happen with activities regardless anyway, so you'll very likely have to make sure rest of the app is in the same activity and use fragments for navigation.
This is not possible with this approach. If the current task contains MainActivity and also other activities on top of that, there is no way to send an Intent to MainActivity (without removing the activities on top of MainActivty) using startActivity or by launching an Activity.
If you need to get information to MainActivity then you need to use another mechanism to do this:
Send a broadcast Intent and have MainActivity listen for the broadcast
Use an event bus implementation
Use a "global variable" to pass this information between the 2 activities

launching already launched activity from notification creating problematic activity

i have 3 activities A-B-C .Lets say i am on activity B and from a listview there i launch activity C now if a notification comes which has an intent of launching activity C.
So the problem is I am getting multiple instances of activity C
When i use launchMode singleTop or instance or task the problem of multiple activity instance is solved but the newly launced activity does not work properely as it is desired to be.
Please help me out tired of using flags and stuff but not able to overcome this problem.
The scenario is Just like whatsapp , if u r talking to person one and a message of person 2 come as notification ,when u click on that notification the activity relaunches and works properely. This is exactly what i want. Please help me out how to achieve this . :(
Thanxx in advance
What Flags did you try and what exactly is not working, means, how does the Activity behave?
What you describe with WhatsApp could be achieved with two steps:
Use the FLAG_ACTIVITY_SINGLE_TOP for the Activity.
Overwrite the Actvity.onNewIntent(Intent intent) method in the Activity. This method is called instead of creating a new Activity and gives you the new Intent. I assume that the Intent contains all necessary information in the extras.
The user experience should be as follows:
User chooses from the list, which opens the new Activity
He/she can press home or back button from here. This brings the home screen or the list back.
If, for any reason, somebody else calls startActivity for your Activity while it is running, the onNewIntent is called. There you can add code to refresh the content, maybe have an Alert that tells the user that the content has changed, and shows all that to the user.
If the user presses back or home now, he/she will get to the list or home screen
If that is, what you're looking for, then the flag and the method is what you need

Android: Intent open the wrong activity

I'm working on a widget for an Android app and I encounter a problem. When the user click on the widget, I'd like the application to open his first activity (the one with intent-filter: 'action.MAIN' / 'category.LAUNCHER') (I'll call it Activity A).
To do that, I use this snippet (in my WidgetProvider class):
Intent intent = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.view , pendingIntent);
This works very good if my application is killed or if all the activities have been closed (using the back button until the application close)
The problem happen when several activities are open. Imagine the activity stack is like : Activity A -> Activity B -> Activity C.
If I click on the widget, SOMETIMES the application is just brought to front and Activity C is visible instead of Activity A.
It's very strange because this doesn't happen every time. (It seems that it happen after I navigate a lot in activities)
I really need the activity A to open and not another activity whatever how the activity stack was because this activity is like a hub with several links to differents fonctionnalities.
Can somebody help me?
Thank you very much.
PS: Sorry if my English is pretty poor, I'm from Belgium
From a widget you should do more or less the same as the system launcher does with intents.
Try using FLAG_ACTIVITY_CLEAR_TOP in your intent. You will also want to look at FLAG_ACTIVITY_NEW_TASK.
It's better to follow the platform guidelines by the way, which say that you can also inject deep into your app, provided you give the user the right path to navigate up again.
If you want a specific Activity to launch, why don't you just call that activity specifically, instead of dynamically.
You are experiencing issues due to the Activities in your back-stack. Android manages the life-cycle of your Activities in a very special way that you should probably research - http://developer.android.com/reference/android/app/Activity.html#ProcessLifecycle
calling finish() - will always return to the activity that launched the current activity. The only time your app will close, is if there was not an original activity. You could always test for this condition, and if the calling activity is NULL, you could explicitly call you Activity A.

Launcher not returning to the last-used activity in my application

I'm using a custom Launcher application with Widgets that are used to launch (and provide status updates) for an application.
The application consists of a few Activities - let's call them A, B and C for simplicity.
A is the launched Activity. The user proceeds from A to B and then to C (never in any other order).
At any time the user can press the 'Home' button on the remote control and return to the launcher application.
If the user then presses the 'Back' button on the remote control they always return to the Activity they were last using (A, B, or C).
However, if they click on the widget (rather than pressing back) the Activity that is brought to the front seems inconsistent!
So, here is an example of what happens
From (custom) launcher, use widget to launch application
Activity A appears
User presses a button that launches Activity B
Activity B appears
User presses 'Home'
Launcher appears
From (custom) launcher, use widget to launch application
Activity A appears NOT B
Sometimes I get Activity B instead of Activity A - but I'm not sure under what circumstances. I want to get the Activity at the top of the stack to be displayed and never any other Activity (Activity B in the example above).
I've read Google's documentation about the Activity flags ("single-task", "single-instance", etc...) but nothing seemed to jump out as the solution to my problem.
In my case, Activities A, B, C only make sense when run in that order - and it never makes sense to see a previous activity unless you explicitly go back to it.
I'm not sure if the problem is the way the widget is launching the application or whether there is something I need to specify in my manifest or something else.
Any ideas?
Thanks,
Dan
Isn't that what's supposed to happen? Isn't your widget launching activity A? Would you expect it to launch activity B if you are asking it to launch activity A?
(Althought you say that you get B launched sometimes. Isn't this related to the app being forced out of the memory?)
I'm sorry, but this is not an answer, but rather some additional information related to that same question.
I have found a pattern for Activities when relaunched after home button is pressed.
See my question for all my research:
Android Activity Stack is not working as stated in the docs - last activity in task stack not shown
Hope this can be of help...
I have been reading that the suggested fix for this strange behavior is the use of a "Dispatcher" class as Main that will launch the activity based on the application state...or you can also keep track of all the activities opened that need to be restored...but this can become really cumbersome when having a complex UI application design.

Android: How could a service start an Activity without old Activities in stack shown behind

Scenario:
Activity "A" launches Service "S".
Press 'Home'/'back' key to not to display "A".
Some time later "S" start intent Activity "B"
Intent intent = new Intent(context, MyDialog.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Current behaviour: "B" shows up with "A" behind in the scene.
What I want: "B" shows up without "A" benhind in the scene.
Note: Activity B is a Dialog Activity
First, you shouldn't do this. Services shouldn't start activities (in general). You don't want windows popping up on the user unless it's specifically asked for (ie texting app).
You could use the noHistory attribute on activity A if that suits your requirement. http://developer.android.com/guide/topics/manifest/activity-element.html#nohist
But as you're describing it, what you're seeing shouldn't happen, ESPECIALLY if you navigate away from A with the back button.
I believe you should be able to call finish(); as the last line below what you have here. Don't quote me on that though.
You can get answer from Android Service and AlertDialog Activity. Quoting the comment of Albin:
Add launchMode="singleInstance" in the manifest for the DialogActivity

Categories

Resources