I have two entrnces to my app: from menu and using Intent.
In the second case I need to return some EXTRA_OUTPUT by
setResult(Activity.RESULT_OK, myIntentWithData)
finish()
so I need to detect if my app was called by another(finish in that case or continue work otherwise)
If your activity was called using startActivityForResult(), you can use getCallingActivity() method in your Activity. It will return a ComponentName of the activity that started yours (or null if your activity wasn't started with startActivityForResult() method). Then you can get getCallingActivity().getPackageName() or getCallingActivity().getClassName().
More info here getCallingActivity, https://stackoverflow.com/a/5336612/3569545
Use getIntent() method to get the intent that was the reason to start your activity.
Then if it was launched from the apps menu the intent will have action MAIN.
val intent = getIntent();
if (intent.getAction() != "android.intent.action.MAIN") {
setResult(Activity.RESULT_OK, myIntentWithData)
finish()
}
Related
Let me explain detailed; I have notification and this notification opens B activity with two cases.
Cases :
If app is closed. (not running on background)
If app is opened. (on background or front)
Case-1
I click to the notification and it opens the B activity with case-1. When i press back i want to go to the A activity and kill B activity. I dont need B activity anymore. Everything easy from here without using flags. When I'm on B activity and press back two times from here, it goes A activity and then closes the app. My trouble here is, if i open the app from navigation buttons of phone (can't remember the name of this button) app is opening from B activity. That's not what i expected. I want to open A activity. Don't want to see B activity anymore.
Case-2
I click to the notification and it opens the B activity with case-2.When i press back i want to bring A activity to the front, without creating anything new. If i press back on B activity, two times and close the app and then again re-open app from navigation button of phone, want to open app from A activity.
So how can i make this correctly, i tried to use flags (i already read docs) but couldn't get work.
What flags should i use when i open the B activity and onBackPress method of B activity to go A activity as i wanted
This should be achievable by adding
android:launchMode="singleTask"
to the A activity in the Manifest, then you can just open A activity from B activity onBackPressed and you will have A only once in the stack.
If it's not working the way you want, you can create an abstract class that extends Activity and handle the stack in a static object, then A & B must extend this new class
try this
Intent intent = new Intent(context, YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
try this
android:launchMode="singleTask" in android manifest file
You can achive this by adding FLAG_ACTIVITY_REORDER_TO_FRONT
Intent i = new Intent(context, Activity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
You May try this isTaskRoot() Which will return B is root
if it is true then launch A
other wise you may finish B
B Activity
#Override
public void onBackPressed() {
if (isTaskRoot()) {
//call A which is not exist
Intent i =new Intent(B.this,A.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}else {
//Finish B if A Already Exixt
finish();
}
}
You can call B Activity on Notification Click
if A is present then u can finish B else you can launch A
If set FLAG_ACTIVITY_CLEAR_TOP, the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
Just put this in ActivityB onBackPressed:
Intent i = new Intent(ActivityB.this , ActivityA.calss);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
finish();
How solve case 1:
finish(); on ActivityB BackPressed method make ActivityB finish after open ActivityA. So after opens ActivtyA, ActivityB will shut down.
How solve case 2:
With this combination flag, It will do what you want. It will close all activities in stack and just keep destination activity. If instance of activity exist it will use it and calls OnNewInstance and if not it will creates new one.
If this is the only instance of Activity B being used, you can add the flag noHistory to the manifest for Activity B
android:noHistory="true"
This will stop Activity B being added to the back stack, this is also possible dynamically by using the Intent Flag FLAG_ACTIVITY_NO_HISTORY when calling Activity B.
As for having Activity A start when Activity B is killed #Quentin Menini's answer of having a single task activity set in the manifest will work if that is the only way you wish Activity A to be accessed, or the Intent Flag Intent.FLAG_ACTIVITY_REORDER_TO_FRONT as #Naimish Vinchhi has suggested, will have the desired effect in this instance.
https://developer.android.com/reference/android/content/Intent.html
link to see all possible Intent flags
https://developer.android.com/guide/topics/manifest/activity-element.html
link for all possible manifest activity options
How to prevent to create every time Instance of Store activity?
When I call startActivity(new Intent(this,StoreActivity.class)), it will create new instance and call OnCreate Method in StoreActivity. I want to call one time Oncreate.
Is this Possible ?
Do this
startActivity(new Intent(
this,StoreActivity.class).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP));
From Android Documentation
public static final int FLAG_ACTIVITY_SINGLE_TOP
If set, the activity will not be launched if it is already running at the top of the history stack.
Constant Value: 536870912 (0x20000000)
Start your activity like this:
Intent storeIntent = new Intent(this, StoreActivity.class);
storeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(storeIntent);
It will call onCreate() only once on first launch of activity, if activity is already running it calls only onNewIntent() instead of create new instance of activity or calling onCreate.
This is not possible since each time you press back button, onBackPressed() method is called which actually destroys your StoreActivity.
If you want this method to not destroy your activity, just remove or comment out super.onBackPressed() line in this method.
In this case your activity will not be destroyed when back button is pressed, but then you will have to use any other logic to bring your MainActivity to top of the stack.
try to finish the first activity after starting the new one,
add this code after the Intent command.
finish();
like that:
startActivity(new Intent(this, newActivity.class));
finish();
check this link
Finish an activity from another activity
I have an Activity with an EditText and some checkboxes. After the user inserts a text the text should be sent to the Service and the Service will run in background showing a Toast from time to time.
I am having a really hard time trying to figure out how to send the data(Strings and Boolean values that user inputs through the Activity) to the Service .
Use Intent on Activity put values in puExtra
Intent intent = new Intent(current.this, YourClass.class);
intent.putextra("keyName","value");
and then call StartService so the OnStart method call be called..
in service get the values in OnStart by using intent
Bundle extras = getIntent().getExtras();
if (extras != null)
{
String value = extras.getString("key");
}
Have you taken a look at the Android Documentation For Services?
It explains everything you need to know and more :)
(hint: you must pass an Intent to OnStartCommand())
If you google "pass an intent to a service in android" among the first results you'll find:
Pass data from Activity to Service using an Intent
Is this A correct way to manually refresh an activity?
....
case R.id.action_refresh:
Intent i = new Intent(Homepage.this, Homepage.class);
Toast.makeText(Homepage.this, "refreshing", Toast.LENGTH_LONG).show();
finish();
startActivity(i);
seeing that this method destroys the activity and recreates it, i was wandering weither it may be to much of a costly method.
The method given by you is a standard one. But if you need another method you can use the following method
onCreate(null)
I have a view in my android app that needs to open another view
and also close\finish all the calling hierarchy that made it open.
How can i do this?
If i use this code, the finish() is never called no?
Intent intent = new Intent(A.this, B.class);
setResult(RESULT_OK);
startActivity(intent);
finish();
You can clear whole stack using:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
this is a solution if you want to start new activity, and close all activities that were open before it.
Google has an informative article on the Android back stack here.
In your code the .finish() is called and your calling activity should finish, but only that Activity and not others in the back stack. You can maintain some control of your back stack via Intent flags that are explained in the article, i.e.FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_CLEAR_TOP, FLAG_ACTIVITY_SINGLE_TOP.
Another method is to use startActivityForResult(...) and close unwanted Activities in the onActivityResult(..) method in your Activity when it returns to the foreground..