I need to transfer User and int between 2 activities, this happens when clicked on item in listview,
Activity 1:
Intent intent = new Intent(GameActivity.this,ServerActivity.class);
intent.putExtra("serverNum",position);
intent.putExtra("currentUser",currentUser);
Log.d("position", position + "");
startActivity(intent);
finish();
the position is by defult the number of the item that the user clicked on, The log.d shows the currect position indeed, but when I try to get that Int in activity 2 its always 0.
In activity 2:
Log.d("Server Number",getIntent().getExtras().getInt("serverNum")+"");
this log.d always shows 0.
the user transfers well though.
EDIT
I found out it has something to do with android:launchMode="singleTask" in the manifest, for some reason the activity opens twice, one opens well and one for some reason opens with the extra "serverNum" equals to 0, any suggestions on how to fix this?
Try this in your Activity 2.
int id=getIntent().getIntExtra("serverNum", 0);
Log.d("ServerNumber",id+"");
Try overriding method and check value there as well
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
int id=intent.getIntExtra("serverNum", 0);
Log.d("ServerNumber",id+"");
setIntent(intent);
}
You should get rid of android:launchMode="singleTask". The reason is that this creates only one instance of the activity. Once you go back to it, it resets the extra to 0. Here you can read up on launch types: https://inthecheesefactory.com/blog/understand-android-activity-launchmode/en
Related
I have two activities, one is a clicker game (where you click a button and the textView displays an int (increasing per click). The other activity is where I display that int. I've tried many times of different ways of how to pass this data to the next activity, however nothing seemed to work. I assume that this could be because this int does not stay the same, and is constantly changing. Could anyone suggest what to do?
Clicker:
Intent getNumber = new Intent(this, Shop.class);
getNumber.putExtra("passedMoney", clicks);
startActivity(getNumber);
Stats:
Intent getNumber = getIntent();
int clicks = getNumber.getIntExtra("passedMoney", 0);
clicks is the int, which is 0.
Thanks in advance :)
Send data on click
startActivity(new Intent(vContext, OtherActivity.class).getIntExtra("number", itemData));
How to get data
int pos = getIntent().getIntExtra("number", 0);
I have two activities : lets say A and B
A has a button which opens B using intent concept.
Through B I am saving data in database using SQLite concept.
A is the main activity which has a textView which is showing some value !
code:
Using SharedPreferences to store int value and display in MainActivity A
c=sp.getInt(Salaryflag, 0);
str=Integer.toString(c);
tv.setText(str);
Now I am using some algorithm to calculate sum() in one row in the database a store its value in an int variable
Now in onResume I am using this concept to change the value of the A textView ! :
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
ItemsDataBase xyx=new ItemsDataBase(this);
xyx.open();
int lola;
lola=xyx.getSum();
xyx.close();
c=c-lola;
str=Integer.toString(c);
tv.setText(str);
}
Now what I want is that when I go back from second Acitvity B to A it should immediately show the changes.
But actually It is showing some garbage value and when I restart the app after closing then it shows the desired changes in the textView
How to remove this logical error ?
Garbage value:
After closing the app and reopening it:
You should write c=sp.getInt(Salaryflag, 0); in your on resume too as you are assigning c-lola to c.
When you are restarting the activity, line c=sp.getInt(Salaryflag, 0) is getting executed first so it displaying perfactly.
When you coming from B to A , the onResume() of A gets called so you have to initialize/assign variable c in onResume() as well.
I try to send position of array from my MainActivity to another Activity with this code:
Main Activity:
contactinfo.putExtra("position", String.valueOf(position));
I have a toast and that shows the right position I have clicked in this Activity.
Another Activity that gets the data:
position = getIntent().getExtras().getInt("position");
Toast.makeText(this,String.valueOf(position),Toast.LENGTH_SHORT).show();
This Toast always shows 0, even when I have click on position 1 or another one.
What's the problem with converting int to String or sending an int via Intents?
The problem is that you are sending a String, but trying to retrieve an int.
You don't need to convert your integer to a String to pass it to the next Activity.
Change contactinfo.putExtra("position", String.valueOf(position)); to contactinfo.putExtra("position", position);.
In Main Activity Use :
contactinfo.putExtra("position" , position);
In Another Activity Use :
position=getIntent().getIntExtra("position", 0);
I'm working on an app that has two activities.
When I go from one activity to another then return I loss the data.
Imagine:
First activity shows a random number on a text view that changes on a button click
Second activity does nothing it only has a return to the main activity button
*I know this app is worthless but its easier than showing you my large application...
So you have a random number on the textview, I go to the next activity then return to the main activity and the textview loses the number and shows another.
What the question is, is how to return to the activity without lossing the data (random number)?
I dont want to save it on a database or on the sharedprefes since that wouldn't be good when being used in a large app with many data shown...
Thx in advance
Edit:
Code to act1:
//public clas... oncreate...
Onclick(...){
Intent intent = New intent (this, acttwo.class);
StartActivity (intent);
Finish ();
}
Code to act2:
//public clas... oncreate...
Onclick(...){
Intent intent = New intent(this, actone.class);
StartActivity (intent);
Finish ();
}
Your problem cause, because
You are calling finish() in Activity 1 when you starting Activity 2.
So remove finish() from Activity 1. and implement onActivityResult()
Like,
Onclick(...){
Intent intent = New intent (this, acttwo.class);
StartActivityForResult(intent, 0); // Result to ensure about back track of proper activity
}
Now Activity 2 just call finish() no need to call start Activity 1 again as Activity 1 is already in back stack of Application task.
So it should be,
//public clas... oncreate... of acttwo
Onclick(...){
finish(); // We are just finishing Activity 2 not starting Activity 1 again
}
Note:
In future it may happen Android system may kill your Application on Low Memory issue, so better to save your data and state of activity in SharedPreferences or Database.
You need to save your data one way or another. Without using DB or other classical methods(shared preferences) used for saving data in Android, as I see it, you are left with two options:
Pass the data with Intents as extras
Save it using method described in Accepted Answer at Saving Activity state in Android
You could use a static variable in your Activity to hold whatever random number your TextView is meant to hold and set its text to that every onResume() call. Something like;
class MyActivity extends Activity {
private static int randomNumber;
private TextView myTextView;
...
#Override
public void onResume(Bundle b){
super.onResume(b);
myTextView.setText(randomNumber);
}
}
Your TextView should persist the number that you had before. If you were setting these values in the onResume() method, then they would change every time the activity resumes. If you terminate your activity with a call to finish (while calling B) and then start a new activity A again (from B), then it will show a random number
If you have to do either of the methods I mentioned, you can add the values you want to persist in an intent as an extra, pass this to the second activity and then pass it back like so:
// Start activity B and pass the value you want to save in there
Intent A = new Intent(this, ActivityB.class);
A.putExtra("value", number);
// Start activity A from B and pass the value you saved in there
Intent B = new Intent(this, ActivityA.class);
B.putExtra("value", getIntent().getExtra("value");
Then retrieve number and set the TextView in your onResume (or onCreate if you are starting a fresh activity. But in this case do a check to see if the intent starting this activity has an extra named value - if yes, then display it else generate a new random number).
I have a TabLayoutObjectActivity that shows 2 tabs with a different activity(TabActivity1 & TabActivity2).
I have another activity called ObjectActivity. It contains an array and if it is clicked I want the app to change to the TabLayoutObjectActivity (which shows TabActivity1 first) and sends a string from an array that clicked to TabActivity1.
I have tried some code but the app always wants to force close after I click one of the array list.
Intent i = null;
i = new Intent(this, TabLayoutObjekActivity.class);
Intent ii = null;
ii = new Intent(this, TabActivity1.class);
ii.putExtra("name", String.valueOf(menuArray[position].toString()));
startActivity(i);
startActivity(ii);
Please give me your opinion how to do this. Thanks :)
Refer to this. It explains how to solve your problem in detail. onclick even you can handle the issue.