how to use startActivityForResult() with a singletask activity - android

In our requirement, we need to refresh A activity after B activity finish.
but B activity is set as singletask.
Is there any flag or another API can fulfill this requirement?
thank you.

AFAIK, the only way to achieve the thing you want - using broadcasting: when B has been finished - send broadcast and in A catch it.
btw, is it so necessary to use singletask?

If you want to set some result to activity A from Activity 2 then you have two options
1) If you have one activity already in activity stack then you have two options
1=> Use startActivityFroResult() and set result back to calling activity
2=> Make fields in activity A as public static and assign values from activityB and onResume() of ActivityA refresh view of ActivityA
2) If you have not ActivityA in activity stack then just pass the necessary values with the intent and from ActivityA onCreate() method fetch all values from intent and load view

Related

Remove all the previous activites except home activity

How to remove all the activities except HomeActivity.
ex:
A(HomeActivity)->B->C->D
now call B activity again removing C & D activities. I'm sending some data from D activity to B activity through intent. I have next button if i click next button then this process will be done. If I backpress then my process will be D->C->B->A
When you're going from D->B, use intent.setFlag(Intent.FLAG_ACTIVITY_CLEAR_TOP);
It will close all of the other activities on top of it and this Intent will be delivered to the (now on top) old activity as a new Intent.
FLAG_ACTIVITY_CLEAR_TOP
Then you need to create static activity variable in both C & D activity.
Just like this
//create static variable
public static CActivity cactivity;
then initialize this variable in onCreate of Activity.
//initialization
cactivity=this;
Finish that activity where you want
// use this where you want to finish activity
CActivity.cactivity.finish();
Make sure Activity must be running in background other wise you app will crash.
what I would of suggest is a not-so-smooth solution but instead of start activities for all of those replace them with a startActivityForResult, search for the result code and if it is returned from the activity (that is for you to handle of course) then call finish with all activity in the way with the result code until you reach your home activity.

send intent to an already running activity

Main Activity has started activity A and A has started activity B, which is currently visible on the screen: Main Activity -> A -> B.
Now Main Activity receives a broadcast intent and it needs to pass new information from it via A to B without restarting A or B, i.e., the state of A and B should remain as is. For example, if user has written someting to EditText in B, that text should not be lost.
Question: what Intent flags should be used when MainActivity sends intent containing the new information to A and A to B in order to achieve the above described behavior?
Use the activity.startActivity(Intent) method in your Activity B to call activity A ‘s “newIntent(Intent)” method, do with what you need to then :) and maybe make sure in the manifest file the activity is set to SingleInstance
Rather than figthing with intents and launch modes, I decided to save the necessary state of A and B at their onPause() method to a companion object of MainActivity and then restore the state from there at onResume() method of A and B.

Create a Button to Close the Program

My project has 4 activities and users from activity A go to B after that to C and D. I need to create a button in activity D to close program directly because if user has to close all activities ( D ->C -> B -> A-> close) it would be unfriendly.
Register a broadcast receiver in each of the activities, listening for the "close all action", when the button in the last activity is pressed, send that broadcast, so all the activities register will execute their "onReceive" method on the broadcastreceiver, and there all them will be finished as long as they are registered.
This will definitely do the trick, although to be honest is quiet a poor implementation, chances that you are doing something wrong in the navigation are high, maybe fragments or a tab would be better suited for what you are trying, in stead of creating such a stack of activities...
Hope this helps...
Regards!
I think onActivityResult could be the better option.You could finish the activity if required task is being completed otherwise just backtrack on previous activity
You should override onBackPressed() from each Activity and call finish().
Assume the first activity in your application is named ActivityMain. Presumably it will be the oldest one on the stack.
Create an intent to start ActivityMain using the flag FLAG_ACTIVITY_CLEAR_TOP. Set an extra in that intent to indicate this is an application exit, and call startActivity() with that intent. This will clear the stack and get you back to Activity main.
In ActivityMain call getIntent() then check for the exiting application Extra value. If it is set, call finish().
A not so elegant solution:
Instead of calling startActivity, call startActivityForResult, from A to D.
On Activity D, when your button is pressed, set any result (let's say Activity.RESULT_OK) and call finish().
On each Activity (from A to C), override the method onActivityResult to check for the result. If the result is Activity.RESULT_OK, then you set the same result and call finish() again.
If you want, instead of just setting the result, add an Intent with some flag to tell the previous Activities to finish themselves.
Simply do one thing . In your activities add an overriden method onPause
onPause(){
finish();
}
This will close all your activities once you press back from any activity.

Run Activity B from Notification and force back button to run Activity A

I have an Android application that contains two Activities.
Activity A has a button that launches Activity B using Context.startActivity(Intent intent).
There is also a Notification that opens Activity in the same way.
If I start B from this notification and press back button - it just closes B and does not shows A like I go there with normal case.
Is it possible to force B to bo back to A if started from notification without history stack ?
Solution
As stefan and Paul Lammertsma mentioned, the best way is to start A from notification and in A create new intent with B - but not in onCreate() !
I dig this a bit and found that if I set in AndroidManifest a new property for A activity:
android:launchMode="singleTask"
there will be in A activity called
onNewIntent(Intent intent)
And there we should checl if Intent containst extra value passed from notification - and if so, then we call new B intent.
Thank you both and good luck with it for next devs ;-)
I would suggest having the notification call Activity A (instead of B directly) with some flag in its extras bundle. In A's onCreate(), check for the flag, and immediately launch Activity B. This will ensure that pressing back on B will return to A.
An easy way to achieve this would be to actually start Activity A from your Notification with a flag to call Activity B instantly.
So you just have to put an extra to your intent you are starting in your Notification and you have to check in Activity A if this extra exists and if it exists then you start Activity B.
Update: another way, but in my opinion not so good, would be to override the onPause() method of your Activity B and call Activity A there.
Maybe not the prettiest solution, but nevertheless a quick one;
add a boolean extra to the intent launching B, "launchedFromNotification" or something like that.
In activity Bs onCreate() you store that boolean value for later use.
In activity Bs onBackPressed() you can check the value of your boolean and if true, launch activity A before calling finish();
A prettier solution may be to launch activity A from the notification, with an extra telling it to directly launch activity B.

Update the first screen automatically even if it is declared as android:launchMode="singleTask"

I have 2 activities. Activity A and Activity B. Both calling each other though intent. Activity A calls Activity B. Activity B accesses database and sends it back to activity A through putExtra() and getExtra().
Now my activity A is in declared like this android:launchMode="singleTask"
When I go back to activity A I want my this activity A to be updated or refreshed automatically. But to my wonder what I understood on debugging that if I declare an activity as launchMode="singleTask" then it just brings forth the screen to top from the stack. It does not actually go inside the code.
Is the concept what I understood correct ?
The solution I see is have a refresh button and on click of that access the code and update screen. But I do not want to do that. Do you think there is any other alternative ? I do not want to change launchMode="singleTask"
Thanks in Advance.
Try startActivityForResult(intent); and while you have finished in B setResult(RESULT_OK); and finish(); the B activity, and in A onActivityResult(int,int,intent); catch the result code if it is RESULT_OK update your A.
No matter which launchMode, when you switch from Activity B to Activity A, the onResume() method must be invoked. You can put your refresh code there to get your activity A updated.
put your refresh part in the onResume method .Once Activity B completed your onResume method will be invoked in Activity A.

Categories

Resources