In my android activity B, I read values bundled in the intent like this
Bundle bundle = getIntent().getExtras();
Boolean mine = bundle.getString("mine").equals("1");
int pagenum = bundle.getInt("page");
When I start B from another activity A, I give in mine=0,pagenum=0.
And I can read that fine in B.
But then in B, I want to reload the activity, by finishing itself and opening another B. I also need to pass in the new data like this:
private void refresh(Boolean mine, int newpage) {
finish();
Intent myIntent = new Intent(this, AllThreadsScreen.class);
myIntent.putExtra("mine", mine ? "1" : "0");
myIntent.putExtra("page", Integer.toString(newpage, 10));
startActivity(myIntent);
}
When I call this, I make sure that newpage has a value of 1. However the problem is that, after starting the activity, when I read the page value from the bundle, it becomes 0 again...
Does anyone know whats wrong?
Thanks.
You are not "restarting" the Activity. If you call finish() and the lines after still work, then it ins't really finished, is it?
What is happening is the Activity may begin the process of finishing, but then receives a new Intent. Android doesn't actually destroy it.
Also, you don't need to finish the Activity anyway. just use onNewIntent (a little known, little used Activity method).
How to capture the new Intent in onNewIntent()?
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 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.
In my game I am trying to pass the score from the PlayGame activity to the Scoreboard activity using an Intent Extra.
On finishing the game, I go to the scoreboard in this way:
Intent intentScoreboard = new Intent(getApplicationContext(), Scoreboard.class);
intentScoreboard.putExtra("com.example.game.SCORE", score_counter);
startActivity(intentScoreboard);
and then in the Scoreboard class I retrieve it in the onResume() method like this:
Bundle b = getIntent().getExtras();
int score = b.getInt("com.example.game.SCORE");
This works fine the first time, but if I then play another game and on finishing return to the scoreboard, I still get the score from the first game.
What am I missing?
you are missing calling setIntent()
try this:
let your score activity do a finish() if you get back to start a new game
then it should be working
getIntent delivers the intent which started the activity. If the activity is resumed you get not the most recently received intent. See here for the solution: https://stackoverflow.com/a/6838082/1127492
Bundle is not much needed to receive getExtra() values.
In my code, i have used to receive like this,
int score = getIntent().getIntExtra("com.example.game.SCORE",defaultValue);
It should works for your problem. And also it won't gives you the already received values.
Hope it sounds good for you dude.
Let's say I am on an Activity, user clicks a button, and I navigate to a web page.
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("SOME_URL"));
startActivity(i);
it takes me to the web page, and then when I click back to goes to my activity, but it starts it all over again.
So is it possible to do this in a way so that after clicking the back button, it takes me to my activity, but instead of onCreate it call onRestart.
The goal is not going through onCreate again.
That is the normal life cycle of an Activity... you could save the state of the activity and restore it to get the "same" as before.
The main question should be: Why do you not want to go through onCreate? If it goes through it because it was destroyed before and need to be recreated. So not going through it will cause a lot of trouble.
You could set an intent in your activity (setIntent) before calling the web browser, then in your onCreate method, check for if this intent called you back (check the intent name or an extra) and if so, skip the rest of onCreate.
Before calling web browser :
Intent intentForThis = new Intent( "AfterWebBroswer" );
this.setIntent( intentForThis );
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("SOME_URL"));
startActivity(i);
And then in your onCreateMethod
public void onCreate( Bundle b ) {
if( !getIntent().getAction().equals("AfterWebBrowser") ) {
//rest of onCreate
}//if
}//met
Regards,
Stéphane
I have 3 activities in Android like activity_A,activity_B and activity_C. Calling sequences like this activity_A >>>>>>> activity_B >>>>>> activity_C. Currently I am in activity_C and now I want to jump directly to activity_A without entering into activity_B and I also have to pass some data.
HOW ???? Please give some piece of code for it!!!
Thanks in Advance !!!
Arthur
If you want to add a new copy of Activity_A on top of the stack, just call it the same way as you did before. Otherwise, choose a launch mode depending on what you want the first copy of Activity_A to do. See this question (method #2) for an explanation of passing data between activities.
You can use similar code as you used to get from A>>>B and B>>>C.
Or you can use finish() to exit your Activity. In your case, you'd set a flag in C to tell B to finish then call finish() in C. In B, look for that flag in the onResume() method and finish() B if it exists.
From your ActivityC code:
Intent intent = new Intent(this, ActivityA.class);
intent.putExtra("a key string", data);
startActivity(intent);
In ActivityA you can recover the data using:
Bundle extras = getIntent().getExtras();
if (extras != null) {
// here use the getters in extras to retrieve the appropiate data type
// for example if your data is a String the you would call
// extras.getString("a key string");
}
Specific code will depend on the type of data. But you can do something like this:
Intent i = new Intent(getApplicationContext(), ActivityA.class);
i.putExtra("myData", someData);
startActivity(i);