I have 2 activities, the first activity include two buttons and the second activity also include 2 buttons. I need when I click on the first button of the first activity, the second activity started and the first button of the second activity become "invisible" and the second button "visible". again if the user back to the first activity and click the second button, then the second activity started where the second button become "invisible" and first one "visible".
I just need to know how to decide that which button from the first activity is pressed by the user. is there any specific method , or if any way?
I tried doing it by set a variable (int i) as a global variable in the first activity and if the user click the first button then this (i) changes to 1 (Same Thing to Second Button), and I set a condition in the second activity that call this (i) variable as:
MainActivity a=new MainActivity();
if(a.i==0){
//do this
}
else if(a.i==1){
//do this
}
but this doesn't work and the second activity always get this (i) as it's equal to (0).
You should start your second activity by using an Intent. For this Intent you can put some Arguments.
In your First Activity:
Button b1 = (Button)findViewById(R.id.button1);
Button b2 = (Button)findViewById(R.id.button2);
// Set OnClickListeners
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Start Activity
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("buttontohide", 0); // Hide Button 0
startActivity(intent);
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Start Activity
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("buttontohide", 1); // Hide Button 1
startActivity(intent);
}
});
In your Second Activity:
Intent myIntent = getIntent();
int i = myIntent.getExtras().getInt("buttontohide");
You can pass parameters into extra before starting second activity. Use it to tell to second activity which button is clicked. In onCreate() method of second activity analyze extra to define which button must be hidden.
MainActivity a = new MainActivity();
You're initializing the Activity again. But in your case, take a static variable instead.
Do something like this.
In your MainActivity.java declare a static variable
public class MainActivity extends Activity {
public static int a = 0;
// Rest of your code
}
Now from the second activity check whether the value is 0 or 1 like this
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
...
if(MainActivity.a == 0) { /*Do something*/ }
else { /*Do something*/ }
}
}
I assumed that you know all the code how to make a button visible and invisible. first declare an int variable in your first activity and pass it to second activity using getIntent, after that using if else logic capture your variable value and put your code to make the button invisible.
You could use a Bundle to pass data from Activity A to Activity B:
In Activity A:
... onClick() {
...
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("ButtonNR", iButtonNR);
startActivity(intent);
}
In Activity B:
public class ActivityB extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
...
Bundle bundle = getIntent().getExtras();
int iButtonNR = bundle.getInt("ButtonNR");
switch (iButtonNR) {
case 0:
//do something...
break;
case 1:
//do another thing...
break;
}
}
}
Related
This is a snippet of my code. The Textview acts as buttons and have Onclicklistner on them. When cpu1000 Textview is clicked it leads to the cpu_g1000 class for which the code is shown below.
public class Game_1000 extends AppCompatActivity implements View.OnClickListener{
private TextView cpu1000, mobo1000;
TextView cpu, mobo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_1000);
cpu1000 = (TextView) findViewById(R.id.proName_G1);
mobo1000 = (TextView) findViewById(R.id.moboName_G1);
cpu1000.setOnClickListener(this);
mobo1000.setOnClickListener(this);
cpu = (TextView) findViewById(R.id.proNameG1000);
cpu.setText(getIntent().getStringExtra("Processor"));
mobo = (TextView) findViewById(R.id.moboNameG1000);
mobo.setText(getIntent().getStringExtra("Motherboard"));
}
#Override
public void onClick(View v) {
if (v == cpu1000) {
opencpu_g1000();
}
else if (v == mobo1000) {
openmobo_g1000();
}
}
public void opencpu_g1000() {
Intent intent = new Intent(this, cpu_g1000.class);
startActivity(intent);
}
public void openmobo_g1000() {
Intent intent = new Intent(this, mobo_g1000.class);
startActivity(intent);
}
In this class, there are radio buttons. The users choose one of the choices and the choice is changed to strings. The strings are sent back to Game_1000 class. The string then gets substituted with "Choose a processor" to show the new choice. The issue I am having is when I choose a motherboard the processor choice is reverted back to "Choose a processor" and the motherboard choice shows. I need both to show at the same time.
public class cpu_g1000 extends AppCompatActivity {
Button button_save;
RadioGroup rG;
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cpu_g1000);
button_save = (Button) findViewById(R.id.Save_G1_cpu);
rG = (RadioGroup) findViewById(R.id.cpu_RadioGrp);
tv = (TextView) findViewById(R.id.proNameG1000);
button_save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int selected_cpu = rG.getCheckedRadioButtonId();
RadioButton selectedRadioButton = (RadioButton) findViewById(selected_cpu);
String radioValue = selectedRadioButton.getText().toString();
Intent intent = new Intent(cpu_g1000.this, Game_1000.class);
intent.putExtra("Processor", radioValue);
startActivity(intent);
}
});
}
Processor is chosen:
Motherboard is chosen:
Example of choices:
The previous choice are not recored and when a new choice is made the previous choice reverts back to "Please choose a processor" but I need both of the information to show. In my original code, I have more than just 2 choices but I shortened it to make it easier to read.
Start your second (Selection) activity for result by calling startActivityForResult(...) and then when user completes the interaction set your selected data in an Intent and pass that intent to method with desired result setResult(...) then call finish on second activity.
After finishing the second activity you will receive intent data in onActivityResult(...) method of first activity, extract that data from intent and then show it to user.
More Description
Example
Try using Shared Preference. Here's a good tutorial.
For your scenario what you have to do is save Processor and Motherboard selection result or send data form one Activity to another Activity. As after processor selection you are returning to Game_1000 Activity you can use startActivityForResult. This method send back the result to calling Activity via Intent. Thats means if you call cpu_g1000 Activity from Game_1000 Activity and after selecting Processor you can send back the result to Game_1000 Activity. Game_1000 Activity will get the result in onActivityResult method.
Example:
Start cpu_g1000 Activity with startActivityForResult from Game_1000 Activity.
public void opencpu_g1000() {
Intent intent = new Intent(this, cpu_g1000.class);
startActivityForResult(intent, PROCESSOR_SELECTION_CODE);
}
In cpu_g1000 Activity set the result to intent and send back to Game_1000 Activity
button_save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int selected_cpu = rG.getCheckedRadioButtonId();
RadioButton selectedRadioButton = (RadioButton) findViewById(selected_cpu);
String radioValue = selectedRadioButton.getText().toString();
Intent resultIntent = new Intent();
// TODO Add data to send back
resultIntent.putExtra("ProcessorSelection", radioValue);
setResult(Activity.RESULT_OK, resultIntent);
// finishing this activity
finish();
}
});
In Game_1000 Activity get the value sent form cpu_g1000 Activity by overriding onActivityResult method.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (PROCESSOR_SELECTION_CODE) : {
if (resultCode == Activity.RESULT_OK) {
// TODO Extract the data returned from the child Activity.
String radioValue = data.getStringExtra("ProcessorSelection");
// save the result value and update UI
}
break;
}
}
}
For mobo_g1000 Activity use same procedure to get the result back in Game_1000 Activity. Start mobo_g1000 Activity with startActivityForResult and get the value in Game_1000 Activity's onActivityResult method. You have to add another case in onActivityResult method for mobo_g1000 Activity.
Check this and this link for better understanding how this work.
I have Four Activities in my application.
Pressing a button on first three activities Leads To the Fourth Activity. I Used intent For Starting the fourth activity.
In the Fourth Activity. I have A text View Which Changes Its text As Follows: -
if the Activity 4 Was started by Activity 1: The text changes to 0.
if the Activity 4 Was started by Activity 2: The text changes to1.
if the Activity 4 was started by Activity 3: the text Changes to 2.
I Declared an intent like this
For First Activity
Intent intent = new Intent(this, Main4Activity.class);
intent.putExtra("callingActivity",0);
startActivity(intent);
For second Activity
Intent intent = new Intent(this, Main4Activity.class);
intent.putExtra("callingActivity",1);
startActivity(intent);
For Third Activity
Intent intent = new Intent(this, Main4Activity.class);
intent.putExtra("callingActivity",2);
startActivity(intent);
And at Fourth Activity, to fetch the Data I used intents like this.. :
public int checkCallingActivity() {
int callingActivity;
callingActivity = getIntent().getIntExtra("callingActivity", 7);
return callingActivity;
}
But when i run the Programme and Call the Activity 4 from either activity 1,2 or 3, it doesn't take the intent value.. It takes the Default value which is 7 in this case. What is the logical error in the code and how it can be resolved?
Use below code for getting activity name if MainActivity is calling Activity for another Activity
String callingActivityName = MainActivity.class.getSimpleName();
Change this line:
callingActivity = getIntent().getIntExtra("callingActivity", 7);
with:
callingActivity = getIntent().getIntExtra("callingActivity", 0);
Hope it helps.
In your Main4Activity's onCreate() method, try this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
............
...................
int callingActivity = 0;
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
callingActivity = bundle.getInt("callingActivity");
}
textView.setText(String.valueOf(callingActivity));
}
Instead of:
callingActivity = getIntent().getIntExtra("callingActivity", 7)
Do this:
int callingActivity = getIntent().getExtras().getInt("callingActivity");
This should work.
i am facing trouble in passing data in activity's onpause() method.when user clicks a button in one activity, 2nd activity starts.in 1st activity's onPasue method i want to pass some data to other activity.i use intent.putExtra() to save data in onpause().in 2nd activity i use bundle.getString() to retrieve data.below is my code
public class FirstActivity extends Activity {
Intent intent;
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tola_calculation);
b = (Button) findViewById(R.id.button);
intent = new Intent(Tola_calculation.this, Kaat_calculation.class);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(intent);
}
});
}
#Override
public void onPause() {
super.onPause();
intent.putExtra("key", "i am value");
}}
and
public class secondActivity extends Activity {
int value;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kaat_calculation);
Bundle extras=new Bundle();
Intent intent=getIntent();
extras=intent.getExtras();
if (extras!=null) {
String value = extras.getString("key");
Toast.makeText(Kaat_calculation.this,value,Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(Kaat_calculation.this,"null problem",Toast.LENGTH_SHORT).show();
}
}}
problem is in 2nd activity intent.getExtras() returns null first time.i press back button 1st activity resumes.then i click again to go to 2nd activity.this time data is retrieved and intent.getextras() does not return null.can somebody explain this strange behaviour.or i am doing something strange as i am new to android programming.i hope i am able to clearly explain problem
This happens because your 2nd Activity is created before the 1st Activity calls onPause().
Solution: put your extra inside the onClick() callback. In other words (as the other answers suggest) you need to attach data to the Intent before calling startActivity() with that Intent.
In 1st Activity's onPause() method I want to pass some data to
other Activity
There's absolutely no need to do so because your 1st Activity is guaranteed to call onPause() as soon as your 2nd Activity becomes visible. So you already know at this point that your 1st Activity has called onPause().
Consult the official docs on Activity lifecycle for more details.
You should add data to Intent before you start the new activity. So this row
intent.putExtra("key", "i am value");
Should go before
startActivity(intent);
All in all, right code
public class FirstActivity extends Activity {
Intent intent;
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tola_calculation);
b = (Button) findViewById(R.id.button);
intent = new Intent(Tola_calculation.this, Kaat_calculation.class);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
intent.putExtra("key", "i am value");
startActivity(intent);
}
});
}
#Override
public void onPause() {
super.onPause();
}
}
onPause method does not mean that your Activity is finishing, merely that it is pausing. If the user is pressing the back-button for example it will Pause, but not finish the Activity. better see the Activity lifecycle documentation
and also see this http://developer.android.com/reference/android/app/Activity.html#finish%28%29
It's NOT working the first time because when you are calling startActivity the extra is not set yet. Activity #2 starts before onPause is called.
It is working the second time because you made the intent variable an instance variable and you are not calling finish() after startActivity() call. That makes your first activity instance stay alive. The second time you launch the activity #2 the intent instance is already there and it already has the extra in it.
Solution: you should put the extra right before calling startActivity() and it'll work fine.
There is a great 7 step explanation on how this all works on Android Docs page
Please solve this problem in android studio, I am new to app development.
I want to create 3 buttons in a single page and navigate each button to each different page.
I need code for java i.e "Mainactivity.java"
I have declared 3 button id's
I have set everything in app manifest.
I am able to navigate only single button at once but how can I arrange all three buttons for navigation?
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonWRGL = (Button)findViewById(R.id.buttonWRGL);
Button buttonHNK = (Button)findViewById(R.id.buttonHNK);
Button buttonKZP = (Button)findViewById(R.id.buttonKZP);
buttonWRGL.setOnClickListener(this);
buttonHNK.setOnClickListener(this);
buttonKZP.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.buttonWRGL:
break;
case R.id.buttonHNK:
break;
case R.id.buttonKZP:
break;
}
}
}
Your question seems unclear, but I suppose that you are asking how to load another layout/activity/fragment by clicking on a button.
Well, it depends on which of the three actions you want to do:
1) in order to load another layout, you need to inflate the new layout in your view; in order to do that you need to clear the actual layout and inflate the new one.
Here's some sample code:
//you may change it to whichever layout you used
LinearLayout ll = (LinearLayout) findViewById(R.id.mainLayout);
//remove previous view
ll.removeAllViews();
//set the new view
setContentView(R.layout.new_layout);
2) in case you want to start a new activity, you need to use a Intent and load it.
Sample code:
//create the new intent; it will refer to the new activity
Intent intent = new Intent(this, NewActivity.class);
//pass any data to the new activity; cancel this line if you don't need it
intent.putExtra(extra_title, extra)
//start the new activity
startActivity(intent);
3) in case you want to change fragment, you need to perform a transaction.
Sample code:
//create the new fragment
Fragment newFragment = new MyFragment();
//start a fragment transaction
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//replace the old fragment with the new
transaction.replace(R.id.frame, newFragment).commit();
Hope this helps; if not, try to edit your question in order to clarify what you mean.
EDIT:
You should add a new OnClickListener to each button, but I would do it differently than you are doing right now.
I would do something like this sample code:
buttonWRGL.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(this, NewActivity1.class);
startActivity(intent);
}
});
For each button. In this code, I directly attach a specific OnClickListener to the button; it will contain the intent that you need.
You can copy this in every button, even 10k buttons, you just need to change the activity name inside the intent declaration with the activity that you want to launch.
I've a Start button in my MainActivity. If I click on this button I go to the next Activity (InfoActivity). Now, I want to remove the MainActivity from the BackStack, if I click on the button. I've tried this:
View.OnClickListener startButtonListener = new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, InfoActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
};
But that removes not the MainActivity from the BackStack, it removes the InfoActivity from the BackStack.
I know that I could insert the flag into the AndroidManifest. But that is not possible for me, because if I go to the PreferencesActivity from the MainActivity and I using the flag in the AndroidManifest, the MainActivity is removed if I return from the PreferencesActivity back to the MainActivity.
So I want to remove the MainActivity from the BackStack, if I click on the button Start.
Add finish();
Like this :
View.OnClickListener startButtonListener = new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, InfoActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
this.finish();
}
};
As said you should use Intent.FLAG_ACTIVITY_NO_HISTORY on the activity you dont want to track in your case on MainActivity.
Other way to do it is Intent.FLAG_ACTIVITY_CLEAR_TOP on your Info activity this will clear whole history (even things before MainActivity)