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();
Related
I'm working on an android application and in the application I have a couple buttons that let user to pass to another activity. Now the way I'm doing the transitions between this Intents is like below:
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
With the above content I start an activity and from that activity I'm getting back to the MainActivity using this code:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
But i suspect this cause memory to be over used because I'm not actually getting back to the Main Activity that created when application started, I'm just creating another instance of MainActivity I guess. Is this really as i thought and if it is how can I get back to the activity that created in the beginning or if I can't do such thing how can I make app to let the previous activity go?
Passing an intent to startActivity() will create a new instance of the activity and add it to the front of the stack. So:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
is basically asking to create a new instance. If you want to go back to the activity just before the current one, call either:
finish();
Or,
super.onBackPressed();
In your solution you just have to press back button and you'll be back in first activity.
If you want to close it and after open new instance like you are doing in second activity just add
finish();
at the end of
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
You just need to call finish(); method
Intent intent = new Intent(this, DestinationActivity.class);
startActivity(intent);
finish();
usually we send data from one activity to another by using:-
Intent i=new Intent("<action name>");
i.putExtras("name",data);
startActivity(i);
My question is can we send data from one activity to another by using:-
Intent i=this.getIntent();
i.putExtras("name",data);
setResult(Activity.RESULT_OK,i);
finish();
If yes please explain the concept.
Also,these two classes are in the different projects in Eclipse.My another question is, is it possible to send data through intent to another activity situated in another project??
So you're starting from Activity A and going to Activity B using startActivityForResult()
Now we're in Activity B and want to go back to Activity A:
Intent i = new Intent();
if(getIntent().getExtras() != null) i.putExtras(getIntent().getExtras());
setResult(Activity.RESULT_OK, i);
finish();
Doing something like that will allow you to pass back the extras from the calling Intent (if they exist), which would then be reachable from Activity A's onActivityResult() method.
Yes. You can do so, when you call another intent for a result. ie startActivityForResult(Intent,Request_Code);
I have the following requirement:
Activity A ---> Activity B ---> Open Gallery App
Traditionally, i launch nested activities using the TaskStackBuilder. So I would do something like this:
TaskStackBuilder tsb = TaskStackBuilder.create(this);
Intent activityIntentA = new Intent(this, ActivityA.class) // ...
tsb.addNextIntent(activityIntentA);
Intent activityIntentB = new Intent(this, ActivityB.class) // ...
tsb.addNextIntent(activityIntentB);
Intent galleryIntent = new Intent(Intent.ACTION_PICK);
galleryIntent.setType("image/*");
tsb.addNextIntent(galleryIntent);
// this.startActivities(new Intent[] {activityIntentA, activityIntentB, galleryIntent});
tsb.startActivities();
(A side question is if there's a difference between using a task stack builder or the startActivities() call).
The problem with this approach though, is that when the galleryIntent is closed, it doesn't call onActivityResult but rather calls the OnCreate method of ActivityB, which means i lose the information coming in from the gallery app, which is supplied through the intent param "data" on my onActivityResult call of activityB.
An alternative solution, would be to manually kick off the calls, so call first call Activity B, then with a flag/param/argument, launch the galleryIntent, and then follow the regular flow through with OnActivityResult.
Is there a better approach to solving this requirement?
I have the feeling that TaskStackBuilder doesn't adapt very well to your needs. I'd approach it in an easier manner.
*I'm assuming the interaction starts on activity A, and then you need to open the gallery, but need activity B to process the result.
I'd open activity B and launch the intent for the gallery from there. As soon as gallery delivers a result to B you can do any processing there.
After the extra processing and if you need to, you can always deliver another result from Activity B to A.
Note that you need activity B to be already created and listening for results before the gallery gets open.
This is my first post, so please be nice :)
I have a question and no one gave the answer in the post I've seen.
My app has a list and a button to open an activity, this activity creates a new item to show in the list of the previous activity when pressing the button Create.
How can I do this?
This is the code I made for the first button:
intent = new Intent(this.getBaseContext(), NewTestActivity.class);
this.finish();
startActivity(intent);
and this is the code to go back an refresh:
Intent intent = new Intent(getBaseContext(), TestListActivity.class);
startActivity(intent);
But the code to goback is useful, because the activity don't refresh.
I have to call the new activity in a diferent way? Or go back to previus activity in a diferent way? Or go back normally and refresh the activity when I'm back in the previus activity?
Well...this is all.
Sorry for my bad english and, if this question has been answered in another thread, give me the link to read, because I can't find it :)
PS: I started with android in December.
Thanks for your help.
When you are going to start new activity you shouldn't close a current one until you actually need this behaviour. (remove this.finish(); row from your code)
Also you shouldn't close an active activity manually until you actually need it. When the user presses the "back" button on the device Android pops the previous activity from the "back stack". Read once more Activity documentation.
According to your description you have a list of elements. So in order to refresh the list you need to update your dataset and notify the list about it by ListView.notifyDataSetChanged() method call.
Before getting to a real answer, I'd like to show some improvements to your code.
First, when creating an Intent (or when you need a context in general) from an Activity there is no need to call getBaseContext(), you can just use this:
intent = new Intent(this, NewTestActivity.class);
Second, android is good at handling Activities, you do not have to close your first activity manually with finish(). Android will automatically pause or stop your first activity and bring it back when you return to it.
Third, in your case you might want to use startActivityForResult() instead of startActivity() for reasons I will explain below.
This will make your code look like the following:
private static final int MY_REQUEST_CODE = 33487689; //put this at the top of your Activity-class, with any unique value.
intent = new Intent(this, NewTestActivity.class);
startActivityForResult(intent, MY_REQUEST_CODE);
Now, the startActivityForResult() starts an activity and waits for a result from that new Activity. When you call finsih() in the new Activity you will end up in the first Activitys onActivityResult()-method, with data supplied from the new Activty that is now closed:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode != MY_REQUEST_CODE) return; //We got a result from another request, so for this example we can return.
if(resultCode != RESULT_OK) return; //The user aborted the action, so we won't get any data.
//After the above if-statements we know that the activity that gives us a result was requested with the correct request code, and it's action was successful, we can begin extracting the data, which is in the data-intent:
Item item = (Item) data.getSerializableExtra("customData"); //casts the data object to the custom Item-class. This can be any class, as long as it is serializable. There are many other kinds of data that can be put into an intent, but for this example a serializable was used.
itemList.add(item); //This is the list that was specified in onCreate()
//If you use an Adapter, this is the place to call notifyDataSetChanged();
}
For this all to work, we need to do some things in the second Activity:
When the item has been created, we must set a result:
//We begin by packing our item in an Intent (the Item class is an example that is expected to implement Serializable)
Item theCreatedItem; //This is what was created in the activity
Intent data = new Intent();
data.putSerializable(theCreatedItem);
setResult(RESULT_OK, data);
finish();
This should return to the first Activitys onActivityResult()-method with the item, as explained above.
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.