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.
Related
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
I have three fragments in two activities;fragmentA in activityA and fragmentB,fragmentC in activityB.when i click a button in fragmentA i want to go specifically to fragmentB (not fragment c) in activityB. please help me in this regard.
add this in OnCreateView of Fragment A:
Button button = v.findViewById(R.id.mybutton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ActivityB.class);
startActivity(intent);
}
});
I have two Activities: Activity A and Activity B. Activity A consists of a custom ListView and a custom ListView Adapter. When chosing an item of the ListView Activity B appears and displays the user relevant information. When pressing the back/return button Activity B calls finish().
I do not intend to create as many activites as items the ListView has. I want Activity B to be able to change it's layout id. So far my application crashes on every attempt I make to turn the setContentView method variable.
relevant code from Activity A :
// code
if (position == 0 && imaginaryInt == 3) // item position in the ListView
{
ActivityB b = new ActivityB();
b.setLayoutID(R.layout.main_c); // this line causes the crash
Intent intent = new Intent(view.getContext(),b.getClass());
startActivityForResult(intent, 0);
if (position == 1 && imaginaryInt == 3 ) {...}
Acivity B :
public class ActivityB extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_b);
}
#Override
public void onBackPressed() {
Intent intent = new Intent();
setResult(RESULT_OK,intent);
super.onBackPressed();
}
}
Overriding setContentView() in ActivityB and changing params to R.layout.main_c did work!
Unfortunately I ran out of ideas how to manage to change the the layout of ActivityB from ActivityA.
Any help is highly appreciated.
EDIT: ---SOLUTION---
To be able to change the layout od ActivityB from ActivityA I gave each ListView click a static unique id. In ActivityB the layout is simply changed with the setContentView method and if the row with the unqiue id was clicked.
You can pass the information you want through the Intent used to start the Activity. For example:
if (position == 0 && imaginaryInt == 3)
{
Intent intent = new Intent(view.getContext(),ActivityB.class);
intent.putExtra("layout_id", R.layout.main_c); // pass the id as an int extra
startActivityForResult(intent, 0);
}
And then, in your ActivityB class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int layoutId = getIntent().getIntExtra("layout_id", R.layout.default_value);
setContentView(layoutId);
}
You cannot operate on layout of ActivityB from ActivityA.
on this line:
b.setLayoutID(R.layout.main_c);
b is NOT EXISTENT at the time you are trying to call. It will exist only after startActivity.
And even then you, as I remember, cannot do that from activity A - activity B is not synchronized with A.
Use other means: for example create service to signal ActivityB to change layout.
On button click event I'm using setContentView(R.layout.activity_main); it works correctly.
When if I try to start new activity with Intent and startactivity commands it loads layout twice it looks like layout loading correctly then 1 second same layout loaded again.
Before start activity its loaded single time.
show.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
String selected = spinner0.getSelectedItem().toString();
if(selected.equals("Item 2"))
{
Intent intent = new Intent(second_layout.this,MainActivity.class);
setContentView(R.layout.activity_main);
startActivity(intent);
}
}
});
I'm guessing this line is the problem.-
setContentView(R.layout.activity_main);
setContentView will just change the layout for the current activity, so you're changing the current layout to activity_main, and then you open the Intent for MainActivity class.
Just remove that line.
When you are starting a new activity, there is no need of setContentView while starting the intent.
The intent which is getting started will have the code for loading the layout. So please remove this line.
I hope, in your MainActivity.class, you will already be writing setContentView(R.layout.activity_main) and this is enough for showing the required layout. So remove this extra line which you have included while starting the intent.
You can not set your second activity layout in your first activity before starting your second activity. It will set automatically in your second activity's onCreate() method. So you should write setContentView(R.layout.activity_main); in your MainActivity's onCreate() method. Just remove it from the onClick listener.
So write in your onClick as below:
show.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
String selected = spinner0.getSelectedItem().toString();
if(selected.equals("Item 2"))
{
Intent intent = new Intent(second_layout.this,MainActivity.class);
startActivity(intent);
}
}
});
And in your MainActivity you have to set your layout as below:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Am relatively new to android, and this is the issue am having, i have a main layout which i would call layout A and another which i will call B for the purpose of this question, i included layout B in layout A with a include tag, layout B serves as a header in layout A, but layout B comes with a button, this button is not clickable in layout A, Please how can i make this button from layout B clickable.on the click of the button it is suppose to start another activity. thank you for your response. i will wait here for a while for any other questions you might have.
This is what you need to do to jump to second activity from your current activity.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);// id of your button in layout.xml
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent myIntent = new Intent(CurrentActivity.this, SecondActivity.class);
CurrentActivity.this.startActivity(myIntent);
}
});
}