Get parameters from the intent used for onResume() - android

I'm using a LocalActivityManager to have activities in different tabs, when I switch from a tab to another one, I start the Activity corresponding to the tab selected.
My problem is simple :
if I click on tab 1, I create intent11 and the first time, the method onCreate(Bundle emptyBundle) of Activity1 is called.
If I click on tab 2, I create intent2 and the method onCreate() is called.
Then, when I click on tab1, I create intent12 , the method onCreate(Bundle emptyBundle) is not called but onResume() is called (normal behavior).
I put special extras in the intent11 and intent12 to create Activity1, so I access it using getIntent().getExtras().
My problem is : the second time I go to the tab1, the intent12 is used to start the Activity, but the result of getIntent() is still intent11.
So I can't retreive the extras set in intent12, I can only retreive the extras set in intent11.
What am I doing wrong ? Should I avoid putting extras() in the intents ? Thank you.
Thank you.
PS : for the moment, I set a special flag to my intent to force to call onCreate(), but I'm sure it's not the good way of doing it.

I believe what you are looking for is here:
https://developer.android.com/reference/android/app/Activity.html#onNewIntent%28android.content.Intent%29
onNewIntent(Intent newIntent) allows you to override the previous intent that was used to create/resume the app with the newest intent.

In Xamarin.Android / Monotouch I just added the following method to my Activity and it worked smoothly.
protected override void OnNewIntent(Intent intent)
{
base.OnNewIntent(intent);
Intent = intent;
}
The principle should work fine also in Native Android.

no you should be able to still put the extras but I'm wondering if the extras are getting 'overwritten' when you are creating the new intents so I suggest trying this:
Put your extras into the bundle for the first intent you create, then before creating the next intent set your bundle to whatever might be in the bundle already by doing
Bundle bundle = getResultExtras(false);
Then you can create your new intent then when you are ready to get your data out of the bundle you can do
Bundle bundle = getResultExtras(false);
again and then get your data like you normally would from the bundle, just make sure that the extras your put in Intent1 don't have the same key name as the extras you put in Intent2
hope that helps some.
if you need more specific help it might be useful to post your code.

Related

What is meant by getIntent().getExtras()?

I am new to Android development and I have come across something new while using Intent as getIntent().getExtras().
Can anyone please explain me how can we write getIntent().getExtras(),cause till now what I know is we can call method by creating object of that particular class but here we are call method getExtras() by using getIntent()method.
getIntent().getExtras() is used to get values from intent that are stored in bundle.
Intent class is used to switch between activities. But sometimes we need to send data from one activity to another. So, at this particular moment we need to set some values to intent that can be transferred to destination activity. We can achieve this by the following code -
Bundle bundle = new Bundle();
bundle.putString("key1","someValue");
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
intent.putStringExtra("key","value");
intent.putExtras(bundle);
startActivity(intent);
Now, in the second activity we can get the value of "key" so we can use that in second activity. To do so, we use the getIntent().getIntent can store a Bundle. Let's see an example -
Intent intent=getIntent();
Bundle valueFromFirstActivity = intent.getExtras();
String valueOfKey = intent.getStringExtra("key");
String valueOfKey = bundle.getString("key1");
So this way, one can get values from activities. Bundle is a class that can hold values within itself and that instance of bundle can be given to intent using putExtras(). It is quite helpful in transferring the custom array list.
we can call method by creating object of that particular class
getIntent() is a method that returns an Intent object. When you call getIntent().getExtras(), first an Intent object is returned and then getExtras() method is invoked.
This way of calling is referred to as method chaining(Fluent Interfaces)

Pass Value from First Activity to Third Activity via AsyncTask

I have an Online Ordering System from where users can complete the order in, at least, three steps. Each step below is a separate activity:
Make selection of a Plan
Select the Starting Date
Select Contents based on selected Plan in Step 1.
Now what I have done in step 1 is, once user clicks on a Plan and Order, below code runs onClick.
Intent intent = new Intent(context, SelectDate.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle extras = new Bundle();
extras.putString("pass_selected_meal_plan_id",mp_id_pass);
extras.putString("pass_selected_meal_name",mp_name_display);
intent.putExtras(extras);
context.startActivity(intent);
Toast.makeText(context, mp_name_display+" Selected",
Toast.LENGTH_SHORT).show();
No in the second Activity, I am asking the user to select the order starting date. That is also simple enough and have no issues in it. But the issue begins when I am trying to switch to the third activity.
Since my third activity content is depending on the Plan selection made in the first activity, therefore I have to transfer the selected ID to the third activity. I have it in Extra Bundle in second activity as "pass_selected_meal_plan_id".
The way I am loading the third activity is also crucial. There are two possible ways to start the third activity. First is very simple by putting onClick on the next button. I assume that it will be very simple to transfer the required selection as well. But in this case, as far as I know being a beginner in Android App Development, I will have to give a button to click and load content based on the selected Plan.
The second way is what I want to use. I am running an AsyncTask in the second activity onClick Next. It is working perfectly fine. The only this is I am unable to getExtras in onPostExecute from the previous Activity, i.e. first activity.
I am open to even using Global Variables, but don't really know how to do. Knowing what Global Variables are, I am not too keen to use them instead.
Here is my onPostExecute code:
#Override
protected void onPostExecute(String result) {
Intent intent = new Intent(ctx, SelectMeals.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle extras = new Bundle();
//Instead of "1" I want the value from previous Activity, which already available in Extras from previous Activity
extras.putString("pass_selected_meal_plan_id", "1");
//This is the second value I want to be transferred from first activity to the third activity
extras.putString("pass_selected_meal_name","Test");
intent.putExtras(extras);
ctx.startActivity(intent);
}
Would really appreciate if anyone can help.
You can use below code
In your second activity
// declare global variables
String id;
String name;
Use below code inside onCreate
Bundle bundle = getIntent().getExtras();
//Extract the data…
id = bundle.getString("pass_selected_meal_plan_id");
name = bundle.getString("pass_selected_meal_name");
Now in your Asynctask, write the code below before launching 3rd activity.
#Override
protected void onPostExecute(String result) {
Intent intent = new Intent(ctx, SelectMeals.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle extras = new Bundle();
extras.putString("pass_selected_meal_plan_id",id); // first activity data
extras.putString("pass_selected_meal_name",name); // first activity data
intent.putExtras(extras);
ctx.startActivity(intent);
}
Finally, you can get these values in third activity.
One important point I would like to highlight is that avoid using generic variable names, e.g. id. It is used just for reference in this answer but it is not a good idea. One obvious problem you may face doing so is with Refactor > Rename. It will change even the #+id in XML files.

Is it safe to reuse an intent?

The Android docs define an Intent as "a bundle of information containing an abstract description of an operation to perform". This suggests that you should be able to reuse a single Intent object multiple times if needed, but I haven't seen any examples showing this is the case/ is safe to do. Is there any reason to NOT do the following:
private final Intent enableBluetoothIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
...
protected void onCreate(Bundle savedInstanceState) {
enabledBluetoothIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 0);
...
}
and then call startActivityForResult(enableDiscoverableIntent, REQUEST_ENABLE_BT_DISCOVERY) in multiple places in the code? What happens if the same intent is started twice?
It is completely safe when you want to use it to do the exact same thing, since an Intent is no more than a bunch of data and instructions. If you want to use the same Intent object for different purposes (for example you have a bunch of tabs and try to set the tabs reusing the same intent but changing the activity they'll launch) you have to be more careful, and I'd recommend re-creating a new Intent object for each.

Android 2.3.3 | Differentiating Intents That Start an Activity

In Android 2.3.3, how does one differentiate Intents that start a particular Activity. For example, if both Activity_A and Activity_B have intents that call startActivityForResult( intent, requestCode), how does Activity_C differentiate between which Activity has started it? Also, I know that one passes a requestCode to the starting Activity, but how does this Activity handle the requestCode? There is no method in Intent that says getRequestCode(). Is the only way to do this to place the requestCode in a Bundle in addition to the method startActivityForResult? Thanks!
Intent API:
http://developer.android.com/reference/android/content/Intent.html
One solution would be to pass along an extra piece of identifying data. For example:
intent.putExtra("activity", "com.whatever.MyActivity");
Then the receiving Activity can read it:
Bundle extras = getIntent().getExtras();
String activityName = extras.getString("activity");
It seems like there should be an easy method call to tell what the sending Intent was, but if so, I'm not aware of it.

working with android intents how to pass arguments between father and the intent h in

Assuming i want to open another activity from my current activity and i want to pass arguments such as in my case difficulty level how do I do it?
newGameButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(countryCityGameMenu.this,GameScreen.class);
startActivityForResult(i, GlobalDataStore.STATIC_INTEGER_VALUE);
}
});
is there a way to pass those arguments in the calling ?
can someone show an example explaining
what send activity should do
what the new born activity should do
As ben already mentioned you can add this data to the intent inside an extra bundle.
An extra bundle stores data as a key value pair of native java data types.
You can add data to the intent via the putExtra methods.
In the new Activity you can retrieve this data via the getExtra methods of the Intent. For example the getStringExtra method.
To get the intent that started the current activity just use the getIntent() method from activity.
You have to use extras like this:
i.putExtra(varName, value);
Not a big fan of this approach... but unfortunately it sends to be the only way to do it in android...
fine.. U can also use StartActivity(intent); Thats enough to pass

Categories

Resources