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.
Related
Okay, so I have a buttonarray which I all instansiate with a for loop. Each button gets an ID aswell as an onclicklistener. The onclicklistener opens a new intent (a new activity which is the same for all buttons) and depending on the ID of the button pressed, the new activity should show a certain pdf file.
The problem is that the last number in my array gets parsed to the new intent. So all buttons get the same ID instead of increase from 0 to 45.
Sheetmusiclist.java (mainactivity):
Button sheetsButtons[];
int id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sheetsmusiclist);
sheetsButtons = new Button[46];
for(int i=0;i<46;i++){
id = i;
sheetsButtons[i] = new Button(this);
sheetsButtons[i].setId(i);
View.OnClickListener mThisButtonListener = new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(SheetMusicList.this, SheetMusic.class);
intent.putExtra("id", id);
startActivity(intent);}};
sheetsButtons[i].setOnClickListener(mThisButtonListener);
}}
Then in the next activity (Sheetmusic.java) I recall the intent and get the ID. Then depending on the ID I show a pdf file. But in all cases the last ID in the array is shown: 45.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sheetmusic);
Intent intent = getIntent();
Bundle bd = intent.getExtras();
if(bd != null){
id = (int) bd.get("id"); //ID here is always 45
}
//a switch here with id -> pdf file name
Anyone got an idea?
Use intent.putExtra("id", v.getId()); instead of intent.putExtra("id", id);.
At the moment you are saving your id variable as global for that class and you are assigning an i to it. After all the Button creations are done and when you click on the Button your id variable remains as the last i from your for loop, that is why you always get "last" elements in your intent.
By doing it with v.getId(); you get an id of clicked Button, which you assign as an i during creation process, so all the ids of Buttons are different.
Hope this helps. Good luck :)
I am trying to pass a value to a text view into a different activity but only when the button in the activity is clicked.
Here is the activity I am trying to set the text...
public static Button yes;
public static final String TEST_KEY = "test";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question19);
yes = (Button) findViewById(R.id.finalYes);
yes.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
startActivity(myIntent);
Intent i = new Intent(Question12Activity.this, HighRiskActivity.class);
i.putExtra(TEST_KEY, "SOME STRINGSSS");
}
});
Here is the HighRiskActivity that is the destination for updating the textview's value...
TextView t;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.high_risk);
t = (TextView) findViewById(R.id.abusedOrNah);
Bundle extras = getIntent().getExtras();
if(extras != null)
{
String value = extras.getString(TEST_KEY);
t.setText(value);
}
My problem is that no text is printing at the desired activity, am I passing in the wrong data?? Any help would be amazing, this is the last step to the app that I am creating :D
UPDATE
I do not want the Question12Activity to be directed to the HighRiskActivity when the button is clicked. I want it to go to the next activity but still be able to pass the text onto the HighRiskActivity once the button is clicked. Sorry for the confusion, hopefully that makes more sense :)
Try to pass data this way:
Intent i = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString(“test”, "Data you want to pass");
//Add the bundle to the intent
i.putExtras(bundle);
startActivity(i);
Now in your SecondActivity class you retrieve data as below:
Bundle bundle = getIntent().getExtras();
//get the data…
String stuff = bundle.getString(“test”);
I don't know what exactly you are trying to achieve by starting the first activity with the statements below
Intent myIntent = new Intent(v.getContext(), Question13Activity.class);
startActivity(myIntent);
But if the Activity you want to start is the HighRiskActivity then the following should fix your issue:
Get rid of the statements related to the Question13Activity mentioned above.
and add the call to start the actual HighRiskActivity like below:
Intent i = new Intent(Question12Activity.this, HighRiskActivity.class);
i.putExtra(TEST_KEY, "SOME STRINGSSS");
startActivity(i); //This line is important to start the new Activity.
So, I guess your major issue here is you didn't start the activity with the startActivity(i); call.
In Activity A you are using the internal intent bundle that is not public to you and private to the intent. In the Activity B you are asking the intent to instead look for a bundle that you provided yourself.
Try something like this in your Activity A
Intent intent = new Intent(context, YourActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("EXTRA1", "string data");
intent.putExtra(TEST_KEY, bundle);
startIntent(intent);
Here is a good writeup on the subject (see answer) Advantages of using Bundle instead of direct Intent putExtra() in Android
You dont need to define two intents in order to connect two Activities. Put the following inside onClick method
Intent i = new Intent(Question12Activity.this, HighRiskActivity.class);
i.putExtra(TEST_KEY, "SOME STRINGSSS");
startActivity(i);
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;
}
}
}
I have a button on an activity called "HomePage". When you click the button, I want it to setText to a TextView called "weaponTitle" on a separate activity, called ItemSelection. Though, when I run the setText, it gives the error:
Attempt to invoke virtual method void android.widget.TextView.setText(java.lang.CharSequence) on a null object reference
So this means it can't find the TextView "weaponTitle". Is there a good way to do fix this? I have made sure I set up everything correctly too!
Here is the sliver of code I forgot to share!
new displayPrices(weaponTitle, "Genuine Freedom Staff");
try this
Firstclass
Intent intent = new Intent(getApplicationContext,SecondActivity.class);
intent.putExtra("KEY",value);
intent.putExtra("KEY",VALUE);
startActivity(intent)
Second Activity
Intent intent =getIntent();
confirm_txt =(TextView)findViewById(R.id.txt);
String txt_put =intent.getStringExtra("KEY");
confirm_tId.setText(txt_put);
Inside oncrete of your HomePage Activity
Button yourbutton = (Button) findViewById(R.id.your_button_id);
yourbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
Intent intent = new Intent(HomePage.this, ItemSelection.class);
intent.putExtra("weapontitle",value);
startActivity(intent);
}
});
Inside ItemSelection Activity's oncreate
Intent intent =getIntent();
String txt =intent.getStringExtra("weapontitle");
TextView weaponTitle = (TextView) findViewById(R.id.textview_weaponTitle);
weaponTitle.setText(txt);
go through this way,
pass this from your first activity,
Intent i = new Intent(Login.this, ChangePasswordActivity.class);
i.putExtra("savedUser", savedUser);
Log.e("username", "--->>" + savedUser);
startActivity(i);
in second activity , get this as below
Intent extras = getIntent();
savedUser = extras.getStringExtra("savedUser");
Log.e("savedUser", "--->>" + savedUser);
Now you can set text as you got it on other activity.
etUsername.setText(userName);
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("weaponTitle","Genuine Freedom Staff");
startActivity(intent);
In the second activity you do:
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
String weaponName = bundle.getString("weaponTitle");
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(weaponName);
I want to pass data from login page to main activity which contains my menus which works fine. But the problem is that suppose a user clicks on a particular menu and open another activity lets say activity A. when the user press the go back button to switch to main activity the bundle gives a null pointer exception. Here is my login intent
Bundle m=new Bundle();
m.putString("userid",userid);
Intent intent=new Intent(login.this,main.class);
intent.putExtras(m);
startActivity(intent);
///////////////
main
String userid;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle b=getIntent().getExtras();
}
Above code works fine but the problem arise when main activity creates another activity A. when Activity A want to go back to parent activity (main) activity gives a null pointer exception.
Intent inte=new Intent(A.this,main.class);
startActivity(inte);
Why not store the userid in SharedPreferences.
Setter:
SharedPreferences pref = getApplicationContext().getSharedPreferences("UserAccount", 0);
Editor editor = pref.edit();
editor.putString("userid", userid);
editor.commit();
Getter:
pref.getString("userid", "default_value");
Clear:
editor.remove("userid");
editor.commit();
You are trying access bundle when returning to Main Activity, but you dont create bundle in your intent. Check for it as follows,
if(getIntent().getExtras() != null)
{
Bundle b=getIntent().getExtras();
}
If you want the bundle in activity A then send the bundle to Activity A and when you are coming back from Activity A then call the intent with bundle to main activity again.
Use this:
Intent i = new Intent(this, main.class);
i.putExtra("Key1", "ABC");
i.putExtra("Key2", "123");
// Starts main.class
startActivity(i);
Now in main.class
Bundle extras = getIntent().getExtras();
if(extras!=null){
String a = extras.getString("Key1");
String b = extras.getString("Key2");
}
Hope this will work..