I'm trying to get result back from an activity i'm starting. the problem is the setResult isn't working as it should :
wortinfo_editsave.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Bundle extras = getIntent().getExtras();
if(extras !=null)
{
Intent intent = new Intent();
boolean neu = extras.getBoolean("neu");
if(neu)
{
db.insert_word(wortinfo_wort.getText().toString(),wortinfo_bedeutung.getText().toString(),wortinfo_beispiel.getText().toString(),wortinfo_type.getSelectedItem().toString(),0);
intent.putExtra("res",0);
setResult(100, intent);
finish();
}
else
{
db.update_word(edit_id,wortinfo_wort.getText().toString(),wortinfo_bedeutung.getText().toString(),wortinfo_beispiel.getText().toString(),wortinfo_type.getSelectedItem().toString(),0);
intent.putExtra("res",1);
setResult(100,intent);
finish();
}
}
}
});
i've traced the program multiple times and all conditions are working correctly but only in one instance(first one, neu==true), setResult causes onActivityResult() to be called. i also have another block of code that tries to set the result and finish the activity but all it does is close the activity without returning any result. i've also tried changing 100 to RESULT_OK or other random numbers but it hasn't worked:
public boolean onOptionsItemSelected(MenuItem item)
{
super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case R.id.edit_wort:
...
case R.id.delete_wort:
db.delete_wort(wortinfo_wort.getText().toString(), wortinfo_bedeutung.getText().toString());
Intent intent = new Intent();
intent.putExtra("res",2);
setResult(100,intent);
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
i've read all the other questions about this but its a bit different since a part of the code isn't working while the if block above it works just fine.
Related
My application contains two activities :
First activity contains :
1.different types of modes
2.intensity
3.CCT
Inside of the if condition not going control.if am selecting the seekbar the it should return true.if it is true means should move to next activity.
can any one help me
mColorTemp = (SeekBar) findViewById(R.id.intensity1);
mScheduler.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
boolean swichAction=false;
if(mColorTemp.isSelected()==true){
swichAction=true;
Intent intent = new Intent(mContext, SchedulerActivity.class);
intent.putExtra("swichAction",swichAction);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
});
Along with this i have button named as Scheduler.
Now am not selecting anyone from first activity and press on the scheduler then it will move to the second activity.In the second activity should show labeled switch on to auto.
If am selecting anyone from first activity then should turn to Manual
Second Activity contains :
Labeled switch in that text contains Auto/Manual
Can any one please help me how to do it.
You can't use mColorTemp.isSelected() to do that. Instead, you has to plug a listener on value changed. If value is changed by user, manual mode can be activated.
mColorTemp.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
mManualActivated = true;
}
}
Then, you can use Extras to pass variables from an Activity to another.
How to "put"
Intent intent = new Intent(mContext, SchedulerActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
intent.putExtra("extra_mode", mManualActivated);
startActivity(intent);
Then, to retrieve the value, add this in the SchedulerActivity.onCreate() :
Boolean manualActivated = false;
Bundle extras = getIntent().getExtras();
if(extras != null) {
manualActivated = extras.getBoolean("extra_mode");
}
////// in first Activity
#Override
public void onClick(View v) {
if(v==btn){
Intent i= new Intent(ChatActivity.this ,MainActivity.class);
i.putExtra("name",0);
startActivity(i);
}
}
//////////// in second activity oncreate method
Intent i=getIntent();
int name=i.getIntExtra("name",10);
demo(name);
////// in demo method
private void demo(int name) {
if(name==0){
purchase();
}
}
////// in purchase method
public void purchase(){
bp.purchase(MainActivity.this,PRODUCT_ID);
showToast("in purchase Method !!!");
}
inApp purchase all classes include in my application but first statement not working and second is working.If I separate run second class without switch work properly.plz help.
Change your onClick() method as shown below:
#Override
public void onClick(View v) {
if(v.getId()== btn){// this can be also used as v.getId() == R.id.btn
Intent i= new Intent(ChatActivity.this ,MainActivity.class);
i.putExtra("name",0);
startActivity(i);
}
}
Hope this helps.
So, I am currently trying to edit the Snake game that android's api provides. I am trying to make it so that after pressing menu->settings->resume, it would resume the game. However, instead of going all the way to resume, after I press settings, it quits and resumes from there. I am using Intents to resume the program.
public boolean onOptionsSelected(MenuItem menu){
switch(menu.getItemId()){
case R.id.settings:
Intent prefActivity = new Intent(this,MyPreferences.class);
startActivityForResult(prefActivity, KEY_RESUME_RESULT);
return true;
}
public void onActivityResult(int requestCode, int resultCode, Intent data){
switch(requestCode){
case KEY_RESUME_RESULT:
if(resultCode==RESULT_OK){
if(mSnakeView.getMode() == SnakeView.PAUSE)
this.mSnakeView.setMode(SnakeView.RUNNING);
}
}
}
This is in MyPreferences.class
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.snake_preferences);
resume = (Preference)findPreference(getString(R.string.key_resume));
onPreferenceTreeClick(this.getPreferenceScreen(),resume);
}
#Override
public boolean onPreferenceTreeClick(PreferenceScreen preferencescreen,Preference preference){
super.onPreferenceTreeClick(preferencescreen,preference);
Intent intent = new Intent();
if(preference == resume){
setResult(Activity.RESULT_OK,intent);
finish();
}
return true;
}
You're explicity calling the method that resumes the game inside onCreate(), which is probably why the game is resuming as soon as you launch settings. I'd suggest not using the method and instead setting up a click listener for your preference:
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.snake_preferences);
resume = (Preference)findPreference(getString(R.string.key_resume));
resume.setOnPreferenceClickListener (new Preference.OnPreferenceClickListener()
{
#Override
public void onPreferenceClick(Preference preference)
{
setResult(Activity.RESULT_OK,intent);
finish();
}
});
}
Also, based on your code onOptionsSelected() doesn't explicitly pause the game (however the UI should be paused considering that onPause() will be called), you might want to look into that.
I have a problem with my code about back button. I've tried a lot of answers here in this site.
I have the mainActivity that calls a second activity with startActivityforResult. This second activity starts bluetooth and show a list of the bonded devices, but if I press the backbutton it stops the app with an error.
public class Main extends Activity implements OnSeekBarChangeListener{
...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String address = data.getExtras().getString(BondedDevices.DEVICE_ADDRESS);
if (resultCode==Activity.RESULT_CANCELED){
Toast.makeText(getApplicationContext(), "Finalizando dispositivos pareados", Toast.LENGTH_SHORT).show();
return;
}
switch (requestCode) {
case DISPOSITIVOS_PAREADOS:
if(resultCode==Activity.RESULT_OK){
mConnectThread = new ConnectThread(address);
mConnectThread.start();
estado = EST_CONECTADO; //informa que esta conectado
atualizaEstado();
break;
}
return;
}
}
But when I am in the second activity, and try to back to the mainactivity just pressing the back button, I get an error on main activity and my app return an error:
public class BondedDevices extends ListActivity {
....
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
if(D) Log.e(TAG, "+++ ON BACK PRESSED +++");
setResult(Activity.RESULT_CANCELED);
this.finish();
}
or like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
return true;
}
}
return false;
}
I have tried a lot of different code, but it still doesn't work. Please someone help me. Thank you.
you should call super.onBackPressed();
like this
#Override
public void onBackPressed() {
super.onBackPressed();
if(D) Log.e(TAG, "+++ ON BACK PRESSED +++");
setResult(Activity.RESULT_CANCELED);
this.finish();
}
When you return from the second Activity, you have not set an Intent as the data for the result in the back button case, yet the first thing you do before checking the value of resultCode for RESULT_CANCELED is try to obtain the address from the Intent, which is null so this will cause a NullPointerException.
You need to reorder the lines in your onActivityResult() to look more like this:
if (resultCode==Activity.RESULT_CANCELED){
Toast.makeText(getApplicationContext(), "Finalizando dispositivos pareados", Toast.LENGTH_SHORT).show();
return;
}
//Do this after checking for cancel
String address = data.getExtras().getString(BondedDevices.DEVICE_ADDRESS);
/* The rest of the existing code */
Also, you should NOT override BOTH the onBackPressed() and onKeyDown(), stick with one of them. You're only causing confusion about which code path gets called first. Even in the case where the result is set with a blank Intent, you will still get a NullPointerException in your existing code because the extras bundle of that Intent will still be null.
I have a onClick listener which starts an activity using intent, but how to make the listener to fire the Activity intent only when the user click five times or more?
public boolean onClick(View v) {
Intent myIntent = new Intent(activity, loginActivity.class);
activity.startActivity(myIntent);
return false;
}
Here I am going to login Activity. How can I get back to previous activity after login successful?
public void onClick(View v) {
String username = Username.getText().toString();
String password = Password.getText().toString();
if(username.equals("guest") && password.equals("guest")) {
lResult.setText("Login successful.");
} else {
lResult.setText("Login failed");
}
}
Have a static variable in program which will increment on each click.
When you click count reach 5 then trigger code to start LoginActivity.
static int i = 0;
#override
public void onClick(View view) {
i++;
if (i == 5) {
i = 0;
Intent myIntent = new Intent(activity, loginActivity.class);
activity.startActivity(myIntent);
}
}
You can try to build a counter which count the clicks and from the 5th clicks let him go forward
To turn back to the previous activity just call
finish();
Add a static counter to your activity.
static int clickCount;
In your onClick:
if(clickCount++<5){return;}
For the fist question just a a counter variable on the class and increment in on onClick() and check it its >= 5 before starting the intent.
int clickCounter;
public boolean onClick(View v) {
clickCounter++;
if (clickCounter >= 5) {
Intent myIntent = new Intent(activity, loginActivity.class);
activity.startActivity(myIntent);
}
return false;
}
For the second question you must take into account whether previous Activity must keep exactly the same aspect or update with user data. Take a look at Activity.startActivityForResult (Intent intent, int requestCode) for calling an activity and get a result value from it.
-