Im new to android so im gonna need a little help.
I created four activities in my app.
First activity is the MainActivity.
I assigned a ListView in the first Activity.
From the listView you are redirected to the second activity with a string which says which item you clicked. It doesn't matter which item you selected, but you will be redirected to that second activity only. Only diffrence is that the values of the strings passed are different.
I used this code in the ListView's onItemClick function:
String item = (String) listView.getAdapter().getItem(position);;
Intent i = new Intent(MainActivity.this, Activity2.class);
i.putExtra("item_selected", item);
startActivity(i);
This code redirects me to the second activiy with the string without any problem.
In the second Activity there are two options "Launch Type 1" and "Launch type 2" inside a radioButton group and a button to perform the function.
So i used this code inside the button's onClick method to determine which Activity to go next:
Intent intent = getIntent();
String item = intent.getExtras().getString("item");
RadioButton launch1 = (RadioButton) findViewById(R.id.launch1);
//The problem code:..
if(launch1.isChecked()){
if(item=="ListView_Item1"){Intent launch1=new Intent(this, Launch_activity1.class); startActivity(launch1);}
}
else{
if(item=="Item 1"){Intent launch2=new Intent(this, Launch_activity2.class); startActivity(launch2);}
}
In Eclipse it shows that there are no errors in the code. But when I run it in the Emulator it starts off fine till it reaches the second activity.
When i click the button nothing is done and it does not redirect to any new page...
:(
Please help me in constructing the "if/else" statements in the button...and please do tell me if there are any better ways to achive the task...
Thanks in advance.
Waiting for replies....
In Java, you compare strings using equals() and not ==.
if(item.equals("ListView_Item1"))
Using == you're comparing references and not content.
Use Object#equals() for checking if an object contains same data as another one and == for comparing if two references are referencing the same object.
use equals() for comparison of String or Objects in java !
so your code should be :
if(launch1.isChecked()){
if(item.equals("ListView_Item1")){Intent launch1=new Intent(this, Launch_activity1.class); startActivity(launch1);}
}
else{
if(item.equals("Item 1")){Intent launch2=new Intent(this, Launch_activity2.class); startActivity(launch2);}
}
Related
There are 3 activities. The text value of Textview in the first and second activity is shown in EditText of third activity.
Mainactivity - Textview1 text
Secondactivity - Textview2 text
Displayactivity - Edittext=Textview1+Textview2
Using intent I have tried to pass the the TextView values of both activities to third activity. In the third activity I concatenated by simply using + in EditText. I am trying to show previous textview values in one paragraph i.e. EditText.
This code is on third activity :
Display activity
Intent intent = getIntent();
String displayingtext = intent.getStringExtra("message");
String displayingsecondtext = intent.getStringExtra("hey");
editText.setText(displayingtext+displayingsecondtext);
displayingtext name: message is from first activity
displaysecondtext name : hey is from second activity
The output displayed is from the first textview and the next word null.
In the code it shows
"Don't use concatenate on setText. Use android resources"
Input:
Textview1= Hello,monday.
Textview2=Bye, monday.
Expected Output:
Editext=Hello,monday.Bye monday.
Use concat() method from java.lang.String insted of "+" operator.
String displayingtext = intent.getStringExtra("message");
String displayingsecondtext = intent.getStringExtra("hey");
editText.setText(displayingtext.concat(displayingsecondtext));
U need to send the "message" got form Activity1 & "hey" in the Activity2 startActivity intent param to Activity3.
It is just a warning as Android Studio Lint tool check the setText method param use a '+' operator. Consider fix it like this:
String resultText = displayingtext+displayingsecondtext;
editText.setText(resultText);
Using greenbot event bus you can do it better...
best example for event bus try it...
OK I got it.. It is saying that you have to put them in resources..
If your application doesn't use multiple language support, then you can simply neglect the warning
So I have the following currently:
public void addProject(View v) {
String pn = "";
Bundle extras = new Bundle();
Intent i = new Intent(mMainContext, AddProject.class);
mMainContext.startActivity(i, extras);
if(extras.getString("projectName") != null) {
pn = extras.getString("projectName");
}
}
This is just an example, the first bit is correct though the if statement may not be but this isn't about that block.
What I have noticed is that if I place breakpoints on the new intent, start activity and then the if statement,
Android seems to skip past the startActivity and go onto the if statement.
If I then remove the if statement and run it again, it then launches the intent.
So what I've realised is that it runs to the end of the addProject block and then actually launches the intent...
Why is this? And more importantly, how I do stop it doing that?
Intended outcome is as follows:
1) user presses "Add Project" button
2) intent launched and user inputs project name and presses submit
3) calling intent then receives the p name for use later in the function.
Why is this?
startActivity() is asynchronous. When it returns, the activity is not yet started.
how I do stop it doing that?
You can't.
Intended outcome is as follows: 1) user presses "Add Project" button 2) intent launched and user inputs project name and presses submit 3) calling intent then receives the p name for use later in the function.
Having separate activities may not be the best plan for this (compared with using fragments).
If you wish to use activities, the most natural approach is:
Use startActivityForResult() instead of startActivity()
Have AddProject use setResult() to create a result Intent to deliver back to your first activity
Have your first activity override onActivityResult(), where it can receive a copy of that result Intent and get the results from it
Note that this still will all happen after addProject() returns. However, you will (eventually) get your results.
The Android documentation does not cover this very well, but this is what it has on the subject.
you must use onActivityResult for such task.
From your main Activity use: startActivityForResults(intent) in place
of startActivity();
In your AddProject Activity when user inputs project name and press the
button .
Intent i = new Intent();
i.putExtra("projectName",userInputTextFromEditText);
setResults(RESULT_OK,i)
In your MainActivity
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data){
pn = intent.getExtras.getStringExtra("projectName","");
}
this way you will find your result
I have this issue that I have a list of A and I can add new A, so I startactivityforresult activity with form for A and there is also a button for selecting AType, so I startactivityforresult another activity with AType list.
Problem comes when I do post back result in AType list.
Intent intent = new Intent();
intent.putExtra("aType", aType);
setResult(RESULT_OK, intent);
finish();
And the result is pushed back to A List instead of A Form.
Is this normal behavior or I am doing something wrong?
If this is normal behavior, what can I do to push back result to A Form instead of A List.
If you need more code - I will provide, but I find it now irrelevant.
I have found out that A Form finishes after startactivityforresult call. But why?
Activity1:
Intent intent-=new Intent(this,myclass.class);
startActivityforResult(intent,100);
Override the method OnACtivity result in Activity 1
Activity2:
setResult(RESULT_OK);
finish();
Turned out I have noHistory="true" in AndroidManifest.xml for activity A Form, that is why it was going back to A List Activity
my context menu has two options Silly and Cool. I want to provide one item named activate to list item silly and second item named deactivate to list item cool.
How can i do that.??????
Help
1.If you want to start the second activity(and activate the settings) when Activate button is pressed you can use Intent to send a String or Integer(0 or 1) to know in the second activity that button was pressed.
You can find more information about sending data with Intent here
2.If you don't want to start the second activity when button is pressed but still activate the settings, you can make another class(Utils.java) where you can define all your settings(vibration, volume, ringtone) in methods and call them like that:
Utils mUtils = new Utils();
mUtils.activateVibration();
mUtils.playRingTone();
mUtils.setVolume(volumeLevel);
Hope it helps!
You can use intent.putExtra - put some information there, for example some object, String, whatever you want.
Then in your second activity just get those extras by
Bundle extras = getIntent().getExtras();
And for example if u have some boolean stored then you do:
boolean something = extras.getBoolean("keyToThatBoolean");
I have created an remote controller app in android. In the main page,there are few keys which on pressing sends a signal from the mobile. First of all it asks for a configuration file and parse the file and save the control options in a spinbox. When a particular key is pressed he corresponding control from the spinbox is selected and the signal is sent.
In next screen i would like to have only the keys which on pressed should select the control in the main screen and it should send the signal. In short i should be able to access all the elements in my main_screen.java.
In this you can access your keys in second screen by sending keys from first screen by click on button through this code
Intent in=new Intent(this,yournextActivity.class)
e.g:- my current class is hello.java and next class is Applet.java then through intent
Intent in=new Intent(hello.this,Applet.class)
to pass data to next class use this...
in.putExtras(key,value);
e.g:- my value is String s="Welcome" then i can pass this s to next class like this
in.putExtras("Yours",s);
key should be any text.....
on second class receive this String through this code
Intent in=getIntent();
String m=in.getStringExtras("Yours");
where m is receiving string and "Yours" is the key that you pass from first class...
if you want to pass data from one activity to another, you can do that with the help of intent
to pass data use putExtras()
Intent intent = new Intent(this, yourSecondActivity.class);
intent.putExtra("key", "Value");
startActivity(intent);
and to receive it in second activity use:
getIntent().getExtras().getString("key");
Note i am explaining above as assuming that i am passing data of string type ! so while receiving data, it depends on type of data you are receiving get that accordingly like i used geString("key");
you could pass things by using intents, but have you considered using fragments instead of multiple activities ?
it could even be better if you actually wish the app to work on large screens.