This question already has answers here:
How to manage startActivityForResult on Android
(14 answers)
Closed 7 years ago.
I have a hitcount that I need to be passed to another activity I know how to receive it but I'm not sure how to format it to make sure it gets passed to the other activity. The activity with the receiver is named GameActivity. This is my receiver:
public void finish(){
Intent returnIntent = new Intent();
returnIntent.putExtra("GAME_SCORE",gameView.getHitCount());
setResult(RESULT_OK, returnIntent);
super.finish();
}
I know I have to create a getHitCount field that will pass it to the intent but I'm not sure how to do this. The relevant field is called hitCount, I've also included where it is incremented.
if (sprite.wasItTouched(event.getX(), event.getY())){
/* For now, just renew the Sprite */
sprite = new Sprite(this);
hitCount++;
}
public void getHitCount() {
}
StartActivityForResult is good as suggested by the previous answer.
For the retrieving part
Well u can get the value using getIntExtra .but it consists of a argument called as default value. for that i suggest you provide the previous game score.
Intent in=this.getIntent();
hitCount = in.getIntExtra("GAME_SCORE",default_value);
Is you use setResult you get the Integer in the method startactivityforresult, I recommend you use a preference for save you number in disk and get this data in any other part in your app
Use startActivityForResult(intent); to get intent from next activity when it finished. You can get result in onActivityResult(..,..,.) method.
Related
This question already has an answer here:
Android's "onActivityResult" mechanism won't work
(1 answer)
Closed 5 years ago.
I am trying to start another activity using startActivityForResult with a negative requestCode i.e.
startActivityForResult(intent,-1);
but as I finish my second activity I am not getting response in my first Activity. however positive request code is working fine and first activity is getting expected response.
I've seen documentation it doesn't say anything about signed request code.
Inside my second activity:
Intent returnIntent = new Intent();
returnIntent.putExtra("resultObj",obj);
getActivity().setResult(Activity.RESULT_OK,returnIntent);
getActivity().finish();
You can change other request code. Example:
startActivityForResult(intent,999);
Change the code like below and try:
Intent returnIntent = new Intent();
returnIntent.putExtra("resultObj",obj);
getActivity().startActivityForResult(returnIntent,1);
getActivity().finish();
get this object in another class like below
ClassName model = (ClassName) getIntent().getSerializableExtra("resultObj");
And that class should implements Serializable
class ClassName implements Serializable {
}
Hope it will work..
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I pass data between activities in Android?
I have three activities in my application and each activity is depending on another. I'm currently using static variables to pass objects and values between these activities. Problem with this is that it get's very confusing fast and it's hard to keep track of when I assigned that global variable a value etc. I'm thinking of implementing an interface between these activities to make the code clearer and easier to understand. Thing is, i'm not entirely sure this is the best way to go so any help or advice would be great.
use putExtra to send info to another activity
send:
Bundle bundle = new Bundle();
bundle.putString(“name″, “username”);
Intent newIntent = new Intent(this.getApplicationContext(), ActivityClass2.class);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);
Receive:
Bundle bundle = this.getIntent().getExtras();
String data = bundle.getString(“name″);
data = username
I believe what you want is the Intent.putExtra() method. There are several methods depending on what kind of data you want to pass. See the documenation here.
to pass data:-
Intent i = new Intent();
i.putExtra("key", "data");
startActivity(i);
This question already has answers here:
How to restart Activity in Android
(22 answers)
Closed 8 years ago.
in an app that I am writing, there is a part of it that allows you to change a curtain setting. the problem is, that this setting won't take effect until the activity is recreated. is there a way to tell the app to restart using the onResume() method (hopefully allowing it to save everything in the onSaveInstanceState())?
This has been posted before:
Intent intent = getIntent();
finish();
startActivity(intent);
As of API level 11, you can also just call an activity's recreate() method. Not only is this cleaner because it is less code, it avoids issues that may arise if your activity was launched by an implicit intent.
Perhaps you could restart the activity as has been demonstrated, but pass in some intent extras to send your string back when it re-starts.
Intent intent = getIntent();
intent.putExtra(STRINGTOSAVE, "Save this string");
finish();
startActivity(intent);
and in your onCreate you would of course want to retrieve the string
Intent intent = getIntent();
String STRINGTOSAVE = intent.getStringExtra(ActivityName.STRINGTOSAVE);
and then use the retrieved string to reapply the textfield and any other actions you need.
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