Is it possible to check that an Activity was triggered by a particular Activity.
For example, I have 2 Activities, Activity A and Activity B and A triggered B using an Intent. And now that I am in Activity B, I want to check that Activity B was triggered by Activity A, like:
if(Activity B was trigger by Activity A)
{
//do something
}
You can use
getCallingActivity();
to get the name of the activity that started the current activity. But you should consider why you need this functionality. Most activities should be loose coupled.
When creating the Intent, you can pass some information, such as:
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("com.mypackage.triggeredby", "ActivityA");
And in ActivityB's onCreate or onResume, you then say:
Intent data = getIntent();
triggeredBy = data.getStringExtra("com.mypackage.triggeredby");
I like Strings, but you could alternately pass the Object, or an int, or anything you can use to identify it with.
Related
I have two activities, A & B. A has a button to go to B. B sets some parameters using Seekbars.
When I go back to A there is no problem. But, I when again go to B, the Seekbars do not show the changed value.
I tried looking for solutions and I came to know about "Intent" class but the concept is not clear to me.
What is a simple clean solution to see the changed values when going to the Activity B again from Activity A?
When we want to move between activities, we use Intent. For e.g. If I have two activities ActivityA and ActivityB, I will do something like this:
Intent intent = new Intent (Activity.this, Activity.class) ;
startActivity (intent) ;
But I want to pass some values from one activity to another, Intents are also useful for that. Lets say I want to pass some string value:
In ActivityA (before starting the activity):
Intent intent = new Intent (Activity.this, Activity.class) ;
intent.putExtra("key", "value");
startActivity (intent) ;
And in ActivityB (inside onCreate) :
Intent intent = getIntent() ;
String test = intent.getString("key");
And here you have your value. You can also pass objects if you want by implementing serializable/parcelable class. Read about it:
How to pass value using Intent between Activity in android
https://www.javacodegeeks.com/2014/01/android-tutorial-two-methods-of-passing-object-by-intent-serializableparcelable.html
You should use intent.putExtra() and intent.getExtra().
You can see the example here; https://stackoverflow.com/a/14017737/8883361
I am blocking in onActivity result.
Activity A startActivityForResult() to Activity B, Some business reason i am removing activity B and moving to till Activity F. From Activity F have to send setResult() to Activity A same time i have to clear stacks while moving to Activity A.
How to handle this scenario?
We can use a flag from Intent class (didn't try myself) -Intent.FLAG_ACTIVITY_FORWARD_RESULT
Right before calling finish() in each of your middle actvities, you must be making a call to startActivity(intent). Pass this flag to this intent:
showC.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
In your last activity, do the regular setResult() ceremony. And then try to get result in your first activity as usual.
using intent start ActivityF and pass result to ActivityA using intent.putExtra
Intent intent = new Intent(ActivityF.this, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("RESULT", "result to ActivityA");
startActivity(intent);
Say I have
Intent secondPage = new Intent(FirstPage.this, SecondPage.class);
Intent thirdPage = new Intent(FirstPage.this, ThirdPage.class);
if(i == 2)
startActivity(secondPage);
if(i == 3)
startActivity(thirdPage);
Are either Intents started if the 'startActivity' method is not called?
Or are Intents only started when the startActivity method is called with that Intent as a parameter
See this link
To quote
To start an activity: An Activity represents a single screen in an
app. You can start a new instance of an Activity by passing an Intent
to startActivity(). The Intent describes the activity to start and
carries any necessary data. If you want to receive a result from the
activity when it finishes, call startActivityForResult(). Your
activity receives the result as a separate Intent object in your
activity's onActivityResult() callback. For more information, see the
Activities guide.
As you question stands, if i is not equals to 2 or 3 then these activites will not be started.
If you not call the startActivity, than the Intent won't start the Activity
To answer your question: No.
An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity.
When you say,
Intent secondPage = new Intent(FirstPage.this, SecondPage.class);
The constructor used here takes two parameters:
A Context as its first parameter (this is used because the Activity class is a subclass of Context)
The Class of the app component to which the system should deliver the Intent (in this case, the activity that should be started)
And,
startActivity(secondPage);
To start an activity, call startActivity() and pass it your Intent.
So when you call startActivity(intent)-- there is no intent which is started. The system receives this call and starts an instance of the Activity specified by the Intent.
Read:
http://developer.android.com/training/basics/firstapp/starting-activity.html
http://developer.android.com/reference/android/content/Intent.html
And in your case, if i is not equal to 2 or 3, nothing happens.
I have got 4 activity let it be A->B->C->D.In every A,B,C activity user need to enter data all data will sent to server in C activity if the user data is correct he will move on to D activity and all the activity A,B,C removed from stack.If the data is in correct i need give the user to reenter data i.e is on back press it has to move C->B->A.My question is How to remove A,B,C activity when user enter D activity.
Use FLAG_ACTIVITY_CLEAR_TOP this shall solve your problem
From the Android documentation:
If set, and 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.
For example, consider a task consisting of the activities: A, B, C, D.
If D calls startActivity() with an Intent that resolves to the
component of activity B, then C and D will be finished and B receive
the given Intent, resulting in the stack now being: A, B.
Use it like
Intent intent = new Intent(getApplicationContext(),
yourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
and also, take a look at this question:
Activity with Flag FLAG_ACTIVITY_CLEAR_TOP (android)
Edit : I thought you want to move to your home activity from D and want to remove all activities from stack
Your stack would be like homeactivity , A , B , C , D
so i gave you this solution as this shall remove all activities in stack on top of your home activity.
If you want to clear the the stack while going to D, for that you can use FLAG_ACTIVITY_TASK_ON_HOME or FLAG_ACTIVITY_CLEAR_TASK
But both of these for api level 11.
The correct answer interesting for me also, but I can offer a solution:
For example you start activity A from O: O->A->B->C->D.
On activity O you can put in android manifest android:launchMode="singleTop"
Then, when data are ok, you can start activity O with flag "FLAG_ACTIVITY_CLEAR_TOP" - it remove from stack A,B,C and will be called method onNewIntent (Intent intent) http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent) in O, where you can start activity D.
You can start activities with startActivityForResult, and call setResult from for example activity D, in C activity you can listen activity result, and related of this result finish activity or not, or call setResalt from C activity ...
When you want to move D activity there you need to check your
conditions and if your condition is satisfied then you need to enter
into your Next activity(i.e., D) .In that case you need to use the
following code..
Intent intent = new Intent(this,D.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
Suppose In on backpress you need to use finish(). to move back i.e., C
-> B -> A.
Try this piece of code with some modifications:
Intent intent = new Intent(this, D.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // To clean up all activities
startActivity(intent);
Try looking for Activity's public method startActivity(Intent i) and finish() here
In usage wise, it should look like this.
Intent i = new Intent(MainActivity.this, MainActivity.class);
startActivity(i);
finish();
Hope this helps :D
I am implementing a simple app. I need to start an activity based on the state of the Activity. Lets take i am using a button to start the activity.
1. If the activity is not started, I need to start XYZ activity.
2. If the XYZ activity is on focus, then i need to close the activity on the button press.
3. If the XYZ activity is not in focus (like onPause) state then, I need to change the button state.
Can you please help me in the flags that i need to use for starting the intent.
Is it possible to get the state of activity before I start that activity?
Try this
Intent intent = new Intent(currentActivity.this, callingActivity.class);
startActivity(intent);
You can use intent like this to call an activity
The button press will need to be captured by each activity separately, so just code the different responses into each different activity.
First off create a MAIN.java activity that is going to house your other activities. Like others have said you're going to have to code the button captures yourself because that should be common sense if you're trying to deal with intents. When you get that together though, you can start a new activity through intent like so:
// allocate new intent, initialized to the activity you wish to launch
Intent i = new Intent(this, ActivityToBeLaunched.class);
// put information into intent
i.putExtras("KeyName", value); // where "KeyName" is simply a reference string
// and 'value' can be anything from boolean - string.
// launch activity and wait for response
startActivityForResult(i, REQUEST_CODE);
Then within your ActivityToBeLaunched.java class you'll have an oncreate that will pull information from the intent like such:
// get intent
Intent i = this.getIntent();
// get information from intent
booleanVariable = i.getExtras().getBoolean("KeyName");
When you're done with this activity simply use;
// create intent
Intent i = new Intent();
// put information into result to send back to parent
i.putExtras("KeyName", value);
// set the result to be returned
setResult(i, ResultCode);
// finish child, return to parent with results
finish();