Why is my intent not passed when back button is pressed? [duplicate] - android

This question already has answers here:
How to pass data from 2nd activity to 1st activity when pressed back? - android
(8 answers)
Closed 4 years ago.
In Activity A, I can view a string. There is an EDIT button I press in Activity A that starts Activity B (and closes A). I pass the string with an intent from A to B in my onCreate code like this:
In Activity A I have:
//for the edit button
edit = (Button) findViewById(R.id.edit);
edit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//open the Edit Activity, pass over the string
Intent i = new Intent(ViewContact.this, EditContact.class);
i.putExtra("category", categoryname.getText());
//start Activity B
startActivity(i);
//close Activity A
finish();
}
});
In Activity B I have:
Intent i = this.getIntent();
category = i.getStringExtra("category");
//set the EditText to display the value of "category" key
categoryname.setText(category);
All works so far. But if my back button is pressed in Activity B to go back to A, I want to show the string again. As it stands, there is no value in the text box.
Back button code in Activity B:
//for the back arrow, tell it to close the activity, when clicked
ImageView backButton = (ImageView) logo.findViewById(R.id.back_arrow_id);
backButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), ViewContact.class);
i.putExtra("category", categoryname.getText());
//Start Activity A
startActivity(i);
//Finish Acitivity B
finish();
}
});
And then when Activity A starts again it should call the intent in onCreate with
Intent i = this.getIntent();
category = i.getStringExtra("category");
categoryname = (TextView) findViewById(R.id.textViewCategory);
categoryname.setText(category);
But instead the text box is empty.
Can you to tell me how I should go about passing the value back from B to A?

Just delete the finish() command

Related

how to disable the previous activity when click on the next activity

I am having multiple activity in my program the flow of it is like MainActivity-LoginActivity-NextLoginActivity etc.,I am applying transparency theme on each activity except main activity so I want when I click any activity it should show MainActivity in background rather than previous activity.
Maybe you want result below?
You open app, app into these activity in order: MainActivity -> LoginActivity -> OtherActivity
And you want to click a button that in OtherActivity, you don't want to return the LoginActivity but MainActivity
If it is, you can do this:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
In this line: intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Activity will use the Launch Mode by singleTask.
If activity stack has MainActivity, it will finish other activity that on top of MainActivity, then return MainActivity.
You can try to give an id to the parent layout in each activity and handle clicks on it.
Something like:
LinearLayout LoginActivity = (LinearLayout) findViewById(R.id.login_activity);
LoginActivity.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(this, MainActivity));
//If you want to close this one
//this.finish();
}
});

From one activity to two other

Is it possible for one Activity to have two others. What I mean is I have 2 buttons for two parent activities - Button 1 and Button 2. When I click on Button 1 it is opening activity 1 which also has activity 2. When I click Button 2 it is opening same activity 1 like on Button 1 and here is the catch. I want 3rd activity of button 2 to be different. Currently it opens 3rd activity which is on button 1. I will try to illustrate it
Button 1(Activity 1) -> listView (Activity 2) -> TextView (Activity 3)
Button 2(Activity 1) -> listView (Activity 2) -> TextView (Activity 3) <- this must be different from above activity 3
I use same Activity 2 for both menu buttons and then activity 3 must be different for both. I cansee from where this problem come. Because second activity on button 2 is the second activity on button 1. But I don't want to make another activity and to load same thing and to duplicate code/activities. Or should I?
Update:
Button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Menu.class);
intent.putExtra("button", "button1");
startActivity(intent);
}
});
Button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Menu.class);
intent.putExtra("button", "button2");
startActivity(intent);
}
});
Here is activity 3 for button1
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act3);
Bundle b = getIntent().getExtras();
if (b != null) {
String button = b.getString("button");
}
}
Here is activity 3 for button2
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act3_1);
Bundle b = getIntent().getExtras();
if (b != null) {
String button = b.getString("button");
}
}
Use this for button 1 onCLick :
intent.putExtra("button", "button1");
For button 2 onCLick:
intent.putExtra("button", "button2");
Pass that value through from Activity 1 to Activity 3, through Activity 2.
In Activity 3's onCreate, use :
Bundle b = getIntent().getExtras();
if(b != null)
String btn = b.getString("button");
You can use this String to know, whether its from button1 or button2.

Activity stack is not clearing after giving Cleartop

I am having 4 buttons at the bottom. Suppose A,B,C,D. These buttons I have created as a widget and including in every layouts. ON each button click I am opening one activity which I have implemented in Common class. When my application starts A will be on selected state, under A I am starting some activities A1, A2, A3. When I press B, B will be in selected state and B1 activity will be started. I am setting CLEAR_TOP flag when I start B1, but the activities I started under A is not clearing. When I click A button again A1 activity will be started and all the activities like A2 and A3(previously started) will be cleared. But B1, B2 are not getting cleared. why it is happening.
public class TabBar implements OnClickListener{
private Context fContext;
//private Activity fActivity;
private Button btnHome, btnReward, btnProfile, btnFav;
private Activity activity;
//private Button venues,orders,about;
public TabBar(Activity activity, Context context) {
this.activity = activity;
btnHome = (Button)activity.findViewById(R.id.btn_tab_home);
btnReward = (Button)activity.findViewById(R.id.btn_tab_rew);
btnProfile = (Button)activity.findViewById(R.id.btn_tab_profile);
btnFav = (Button)activity.findViewById(R.id.btn_tab_fav);
btnHome.setOnClickListener(this);
btnReward.setOnClickListener(this);
btnProfile.setOnClickListener(this);
btnFav.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.btn_tab_home:
Intent int1 = new Intent(activity, HomeActivity.class);
int1.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
int1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(int1);
activity.finish();
activity.overridePendingTransition(0, 0);
break;
case R.id.btn_tab_rew:
Intent int2 = new Intent(activity, Rewards.class);
int2.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
int2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(int2);
activity.finish();
break;
case R.id.btn_tab_profile:
Intent int3 = new Intent(activity, ProfileActivity.class);
int3.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
int3.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(int3);
activity.finish();
break;
case R.id.btn_tab_fav:
Intent int4 = new Intent(activity, FavouritesActivity.class);
int4.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
int4.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(int4);
activity.finish();
break;
default:
break;
}
}
Note, Clear top stack, will clear all the activities of top of some activity from activity stack, if it is present in activity stack. If it is not present in activity stack, then it will push new activity B1 on activity stack.
In your case, Activity B1 is not present in activity stack, so it wont pop any activity from activity stack and will just push a new activity B1 on activity stack.
If you want to Implement TabBar, then you should use TabFragment or TabActivity for the purpose.

How do you make a new activity start when a button is clicked?

I already know how to start a one new activity when you click a button, but I have three buttons on the one layout. And I want each of the three buttons on that ONE activity to link to three other activities.
I have on the activity I have called 'main', Button 1 which is called services and I want to link it to the services activity. Button 2 which is called Search and I want it to go to the Search activity and thirdly 'map' which I want to link to the map activity.
Can someone help me do this please? Thanks
EDIT:Also, I'm a beginner with Android coding, could you explain in a little bit more detail please?
So where is problem? Just set that your class will implements View.OnClickListener and override onClick() method a you got it.
public class MainActivity extends Activity implements View.OnClickListener {
// body
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.serviceBtn:
Intent serviceIntent = new Intent(this, ServiceActivity.class);
startActivity(serviceIntent);
break;
case R.id.searchBtn:
Intent searchIntent = new Intent(this, SearchActivity.class);
startActivity(searchIntent);
break;
case R.id.mapBtn:
Intent mapIntent = new Intent(this, MapActivity.class);
startActivity(mapIntent);
break;
}
}
Tie an onclick listener to all three buttons. In the listener, retrieve the ID of the button. Make a variable of type Class. Depending on the value of the button ID, initialize it to the class of the activity to invoke. Then construct an Intent for that class, and call startActivity().
EDIT for hawaii.five-0: here's how I'd do it:
#Override
public void onClick(View view) {
Class c = null;
switch (view.getId()) {
case R.id.serviceBtn:
c = ServiceActivity.class;
break;
case R.id.searchBtn:
c = SearchActivity.class;
break;
case R.id.mapBtn:
c = MapActivity.class;
break;
}
Intent i = new Intent(YourActivity.this, c);
startActivity(i);
}
EDIT2:
class CurrentActivity extends Activity
implements OnClickListener
{
void onCreate(Bundle b)
{
//Other initialization goes here...
((Button)findViewById(R.id.MyButton1)).setOnClickListener(this);
((Button)findViewById(R.id.MyButton2)).setOnClickListener(this);
((Button)findViewById(R.id.MyButton3)).setOnClickListener(this);
}
}
Its very easy dear. Just put this code on button click event
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);
which is very easy to start a new activity when the button is clicked. I have given the example for this.
// R.id.btnAdd -> which is present in your layout page
Button btnStart = (Button) findViewById(R.id.btnAdd); // declare button
// declare listener evernt for button
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
// declare the Intent for moving another activity
Intent view = new Intent(YourCurrentClassName.this,
anotherClassName.class);
// startActivity is used to navigating the view
startActivity(view);
}
};
// set the listener evernt to button
btnStart.setOnClickListener(listener);

How can I make a button visible in one activity when clicking a radiobutton in another?

Im making a simple Androidgame , where the user selects an answer to a question (ind Activity1) by clicking a radiobutton. When the correct radiobutton is clicked, a button in the "Credits" (Activity2) will get VISIBLE and be available for the user.
How can I make this happen? i canĀ“t get the two activities to work together?
The code from Activity 1 (the Question) where the user clicks the radiobutton:
final Button s1 = (Button) findViewById(R.id.radio0);
final Button s2 = (Button) findViewById(R.id.radio1);
final Button s3 = (Button) findViewById(R.id.radio2);
s1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
btnEliminar.setVisibility(View.VISIBLE);
btnKort.setVisibility(View.VISIBLE);
s1.setVisibility(View.GONE);
s2.setVisibility(View.GONE);
s3.setVisibility(View.GONE);
AlertDialog.Builder builder = new AlertDialog.Builder(Activity1.this);
builder.setMessage("...");
builder.setCancelable(true);
builder.setPositiveButton("...", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
The code from Activity2 where the button should get visible:
public class Activity2 extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Button credit1 = (Button) findViewById(R.id.buttoncredit1);
credit1.setVisibility(View.INVISIBLE);
....
credit1.setVisibility(View.VISIBLE);
Hope someone is able to help me
Thank you
This can be done via Intent's extras. Should look something like this:
//Somewhere in Activity1
Intent intent = new Intent();
intent.setClass(getApplicationContext(), Activity2.class);
intent.putExtra("makeButtonVisible", true); // Or false
startActivity(intent);
//Somewhere in Activity2
boolean isButtonVisible = getIntent().getBooleanExtra("makeButtonVisible");
// Change button's visibility accordingly
To understand the best answer to this question you must understand that each Activity in Android is essentially isolated from any other activities. This means you cannot change things such as widget visibility from one activity to another.
That said, the best solution is likely to pass extras using an Intent when you start up your 2nd activity. That can be done quite easily by following these steps.
I hope this helps!
In Activity1
s1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(this,Activity2.class);
if(s1.isChecked)
intent.putExtra("state",0);
else
intent.putExtra("state",-1);
startActivity(intent);
});
In Activity 2
Intent intent = getIntent();
int state = Integer.parseInt(intent.getExtras().get("state").toString());
credit1.setVisibility(state);
Hope this help
You can do it by different ways:
1) Implement static method in Activity2, and call it from Activity1. Don't change visibility directly - just change some static field of Activity2 and then handle it in onStart()
2) When starting Activity2, put values into Intent and then handle them in Activity2. But keep in mind, that user can use "Back" button, so you need to handle it correctly (for example, you can .finish() activity and then start new one).
First one may sounds easier, but try to avoid such static methods. Second way is better for your app design.
you should use Intent for change a activity to an other activity:
Intent intent = new Intent(thisClss.this, AnOtherClass.class);
startActivity(intent);

Categories

Resources