Transferring int variable between activities Android - android

I am making a quiz game. However, I need to add a score variable that can be modified. The way the game works is it uses a few different activities. How can I transmit this score and modify it throughout the activities.

Here's an example of passing an integer from one activity to another activity.
Start ActivityA and pass it the int like this:
Intent theIntent = new Intent(this, ActivityA.class);
theIntent.putExtra("somename", intVariable);
startActivity(theIntent);
Get the integer from within ActivityA like this:
int i = getIntent().getIntExtra("somename");

trying this do not work in my activity onCreate-method.
Method do need a default value as second parameter.
int id = getIntent().getIntExtra(DetailClick.ID_NAME,-1);
Hope this helps!

Related

How to return to second activity and show the change value I made before

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

Is it Possible to Pass Edittext Value from one activity to another activity without using Intent

Intent intent = new Intent(this, Page.class);
intent.putExtra("arg", getText);
startActivity(intent);
//second activity:
String passedArg = getIntent().getExtras().getString("arg");
enteredValue.setText(passedArg);
To get value from one activity to another activity without using
intents....?? I want to show edittext passed data in other activity
edittext but without using intent...?? Kindly help me is it possible
or not....??
There are many ways: event buses, SharedPreferences, through global object (application class), database, simple file. But why?
Try direct and readable style first.
if you can't bundle the values you want to save in the Intent, then you could use the SharedPreferences. More information here

When passing data between Activities with Intent Extra I keep getting the same value

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.

android switchstatement question

I was wondering, how would I make a switch statement, that when that certain case was triggered, it would open a new screen with text. Would I use an intent? And if so, which one?
Thank you for your help in advance.
When you want to open a "new screen", you probably want to open a new activity. You would create a second Activity-derived class and use the following overload of the Intent constructor with startActivity:
Intent intent = new Intent(this, MySecondActivity.class);
startActivity(intent);
this will explicitly attempt to open a new Activity with the class name MySecondActivity
to pass a String of text from one Activity to another in this way, you can add it to the intent.
String someValue = "Some Value";
intent.putExtra("Some Key", someValue);
and in the code of your other Activity, you can get at this string via the Intent
getIntent().getStringExtra("Some Key");
Obviously you want to do null checks to make sure the key exists in the Intent, and you want to put a proper constant String somewhere instead of using a literal String for a key, but this is the basic gist.
Kriem... As Rich said you can launch a new activity using intents and push data to the new activity using extras. You can push data back to your main activity using startActivityForResult instead of startActivity. You can return to your main screen by calling finish() in the new screen. Finally, you can put the new screen event handlers into the NewScreen.java file.
The overall effect is a near full separation from dependency between the two activities, so that you might be able to easily re-use the NewScreen.java class in another project. I have some code here.

how to call an object of another class

I have a main class which has edittext box. And i have another class in the same source folder. Now i want to use the text typed in main class accessible to the another class.
any help is greatly appreciated
There are different approaches:
1) You could store the value in a static variable somewhere globally in your app (when you leave the activity = onPause) - works ok if your app activities run all in the same process, which by default is the case - unless specified differently in your manifest
2) or use the SharedPreferences (ref. to android dev docs).
3) If your one activity starts the seconds one right after, you could pass the value as an intent extra (ref. to android dev docs):
Intent intent = new Intent(this, A.class);
intent.putExtra("EXTRA_TEXT_TO_REMEMBER", editText.getText().toString());
startActivity(intent);

Categories

Resources