I'm stuck in one problem from yesterday and I can't find a solution. I'm trying to set one variable in a child activity in my tabhost to 0. Basically I have this situation :
Activity A -- sends extra "something" to activity B (B is getting it)
Activity B -- starts Activity C
Activity C -- onButton click sends extra "something" to activity B and finish()
This all happens in First tab.
The problem is when I change the tab and return back to Activity A, than B, that extra "something" is still saving the value which Activity C sends, no matter if I send it from Activity A. Here is an example code :
Activity A -- starts B :
if (ownedCards != 0) {
ownedStampii.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent previewMessage = new Intent(getParent(), OwnedStampii.class);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
previewMessage.putExtra("collection_id", collId);
previewMessage.putExtra("SOMETHING", 0); // this is what I'm trying to se to 0
parentActivity.startChildActivity("OwnedStampii", previewMessage);
}
});
}
This is how I read extra in Activity B :
final int extra = getParent().getIntent().getIntExtra("SOMETHING", 0);
Log.e("", "extra : " + extra);
This is how I send extra from Activity C :
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = getParent().getIntent();
intent.putExtra("SOMETHING", objectID); // objectId of category which will sort the data
getParent().setResult(RESULT_OK, intent);
finish();
}
});
I tried to set int extra =0 on onResume(), onRestart(), onStop().. and etc. it's still not working.
Any suggestions how can I set this variable to 0?
I think you are reusing an existing intent in activity C and putting another SOMETHING into the same intent. Try calling intent.removeExtra("SOMETHING"); before putting it in again. Or send a new Intent from C to B.
Intent intent = getParent().getIntent();
intent.removeExtra("SOMETHING"); // Hox! Add this.
intent.putExtra("SOMETHING", objectID);
getParent().setResult(RESULT_OK, intent);
finish();
You should pass data between Activities through Intent and startActivityResult() and onActivityResult();
This may be tricky while using TabGroupActivity.
Here is a solution which I posted to another question having somehow same scanario.
You are declaring extra as a final variable which means once you create and assign it to something you cant change it afterwards.
So when you are trying to reassign and change the value it's basically not possible cause your variable is final.
Related
have total 3 activites. I pass the data from the first activity like this:
Intent intent = new Intent(getActivity(), Movie_rev_fulldis_activity.class);
intent.putExtra("mov_pos", position + "");
startActivity(intent);
this working fine all data visible to my second activity but i want to display
one filed item to third activity when i click second activity image
here my second activity
youtube_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent youtube= new Intent(getApplicationContext(),PlayYouTube1st.class);
youtube.putExtra("youtubeLink",youtubeLink);
startActivity(youtube);
// Toast.makeText(Movie_rev_fulldis_activity.this,
// youtubeLink, Toast.LENGTH_LONG).show();
}
});
youtubeLink=Reviews_update.revData.get(mov_pos).getYoutubeLink();
Toast.makeText(Movie_rev_fulldis_activity.this,
Reviews_update.revData.get(mov_pos).getYoutubeLink(), Toast.LENGTH_LONG).show();
mov_pos=Integer.parseInt((getIntent().getExtras()).getString("mov_pos"));
in second activity i am getting values but when i send one filed to third activity that value passing null any one please help me i stuck in their,
I want to show YoutubeLink field sencnd activity to third activity how to parse that any one please help
In FastActivity:
Intent in = new Intent(FastActivity.this, SecondActivity.class);
in.putExtra("a", yourdata);
startActivity(in);
In SecondActivity:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String a = extras.getString("a");
}
You can also use any static variable and use it in any class
static int a = 5;
And in any other class
int b = classname.a;
classname is the class where static variable is declared.
Make that variable static and then pass
Your variable object reference get change in the THIRD Activity so, it returns null
I know how to pass data from one activity to another using Intent:
Intent intent = new Intent(getBaseContext(), AddTextNote.class);
intent.putExtra("text", note.text);
I'll ask my question with example: If one activity would pass only a string to another, the second activity will know that everytime it receives intent from the first activity, it contains only a string. But if the first activity passes multiple set of data on various occations to another, what is the best way to distinguish them? I mean let's say first activity will pass an integer if user presses button1, will pass an array of customClass if user presses button2 and will pas float if user presses button3. So briefly there is 3 ways to start the second activity.
How should I know what intent contains? I can check for being null, like:
string myStringParameter = intent.GetStringExtra("myStringParameter");
But it is a not a wise and efficient way. Is there anyway to distinguish them fast and effective?
Thanks
I think that way could do the trick:
public enum State {
BUTTON_1, BUTTON_2, BUTTON_3
}
public class Activity1 extends Activity {
Intent intent = new Intent(getBaseContext(), Activity2.class);
intent.putExtra("text", note.text);
intent.putExtra("state", BUTTON_1);
}
public class Activity2 extends Activity {
State s = (State) intent.getSerializableExtra("state")
switch (s) {
case BUTTON_1:
//get String value from intent and do what needed
break;
case BUTTON_2:
//etc
break;
....
}
}
Hope you got the idea.
Data is identified via strings.
Activity A sends:
intent.putExtra("stringA", ""+BUTTON_A);
intent.putExtra("stringB", ""+BUTTON_B);
Activity B receives:
String stringAdata = getIntent().getStringExtra("stringA");
String stringBdata = getIntent().getStringExtra("stringB");
I am having two activities (activity1 and activiy2) and each activity is having one button each. In activity1 i having a spinner with few options. Suppose i am selecting option 2 from this spinner and i am clicking the button in activity1, then activity2 starts. When i click back button activity1 is resumed and the same option 2 is visible (like i need). Now the problem is that if my activity2 is started and i am clicking a button in it, activity1 is started. But instead of resuming the previous state of activity1 it starts in a way that it has just created and the previous selection is changed. How can i get the same facility like back button (not the facility of going back to previous activity, i mean automatically resuming the previous state of any activity) even when i start the activity again. Simply i need to know how to maintain the previous state of an activity if it is again visited.
It is with this code I go from one activity to another when button is clicked:
Intent intent=new Intent();
intent.setClassName(getApplicationContext(),"com.myapp.activityname");
startActivity(intent);
Kindly help me.I am a beginner in android, so if any one is giving the answer please explain it a bit. Thanks in adavnce
Think I found the answer. Let me tell what I have done in simple words,
Suppose i am having two activities activity1 and activity2 and i am navigating from activity1 to activity2(i have done some works in activity2) and again back to activity 1 by clicking on a button in activity1. Now at this stage I wanted to go back to activity2 and i want to see my activity2 in the same condition when I last left activity2.
For the above scenario what i have done is that in the manifest i made some changes like this:
<activity android:name=".activity2"
android:alwaysRetainTaskState="true"
android:launchMode="singleInstance">
</activity>
And in the activity1 on the button click event i have done like this:
Intent intent=new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.setClassName(this,"com.mainscreen.activity2");
startActivity(intent);
And in activity2 on button click event i have done like this:
Intent intent=new Intent();
intent.setClassName(this,"com.mainscreen.activity1");
startActivity(intent);
Now what will happen is that whatever the changes we have made in the activity2 will not be lost, and we can view activity2 in the same state as we left previously.
I believe this is the answer and this works fine for me.
By overriding an Activity’s onSaveInstanceState event handler, you can use its Bundle parameter to save instance values. Here is an example:
#Override
public void onSaveInstanceState(Bundle outState) {
// Retrieve the View
TextView myTextView = (TextView)findViewById(R.id.myTextView);
// Save its state
outState.putString("My text",
myTextView.getText().toString());
super.onSaveInstanceState(outState);
}
The saved Bundle is passed into the onRestoreInstanceState and onCreate methods if the application is forced to restart during a session. You can then extract values from the Bundle and use them to update the Activity instance state. Here is an example:
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
TextView myTextView = (TextView)findViewById(R.id.myTextView);
String text = “”;
if (icicle != null && icicle.containsKey("My text")) {
text = icicle.getString(TEXTVIEW_STATE_KEY);
}
myTextView.setText(text);
}
It’s important to remember that onSaveInstanceState is called only when an Activity becomes inactive, but not when it is being closed by a call to finish or by the user pressing the Back button.
Download source code or see the entire post
For activity one, you need to use the startActivityForResult() function that returns values to the parent activity which is the activity one.
Intent intent = new Intent(MainActivity.this, Activity2.class);
startActivityForResult(intent, 1);
And to receive the values from activity two without alteration of activity one, add the following code
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String text=data.getStringExtra("text");
vTxt.setText("Name 1: "+eTxt.getText()+ "\n Name 2: "+text);
}
}
}
Activity 2
add the following code to return to activity one and pass some values
Intent intent=new Intent(Activity2.this, MainActivity.class);
Bundle extra=new Bundle();
extra.putString("text", eTxt.getText().toString());
intent.putExtras(extra);
setResult(RESULT_OK, intent);
finish();
Finally you are done
In an given Android activity, I would like to start a new activity for the user at some point. Once they leave the first activity and arrive at the second, the first activity is hidden..
Now my question is
I want to bring back the first activity (i dont want to create a new instance of the first activity but to bring back the already existing instance of the first activity) when a button is clicked in the second activity ...
thanks :)
so simple. integrate the below code in your second activity
Button b = (Button)findViewById(yourbuttonid here);
b.setOnClickListener(new View.onClickListener(){
public void onClick(View v){
finish();
}
});
This will work
You would define the first activity with launchMode="singleInstance", then you would start the activity as usual.
Depending on the usage of your second activity, you could also use startActivityForResult() when you start your second activity...
FirstActivity.java{
private static final int SECOND_ACTIVITY = 0;
openSecondActivity(){
Intent forChildIntent= new Intent( this ,FirstActivity. class );
//data for second activity
forChildIntent.putExtra("userName", getUsrName());
this.startActivityForResult(forChildIntent, SECOND_ACTIVITY);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
switch (resultCode) {
case RESULT_OK: //do something
default:break;
}
}
SecondActivity.java{
goBackButtonClick(){
Intent retData=new Intent();
//set data to pass back ,if required
//retData.putExtra("userName", getUsrName());
setResult(RESULT_OK, retData);
finish();//will take you to the first activity
}
}
I have an ActivityA, which starts ActivityB through Intent's startActivity() method.The context is as below:
A.java
String name = edittext.getString();
Intent i = new Intent(A.this,B.class);
Bundle b = new Bundle();
b.putString("Name",name);
i.putExtras(b);
startActivity(b);
B.java
Bundle bb=getIntent().getExtras();
String namee=bb.getString("name");
In this B Activity there will be Back button which when clicked takes control back to A as below:
back.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0) {
Intent backToDetails = new Intent(B.this,A.class);
startActivity(backToDetails);
}
});
Now control comes to ActivityA. When I again start Activity B from Activity A , the previous value of the name is lost .So, I again get new Value by overwrting old value in Activity B. So, how to save previous name value ?
How to save the state of Activity B?
Can any one help me in sorting out this issue?
Thanks In Advance,
You just have to save the state of your activity B. In this related question there is a complete answer to solve your problem. Good luck!