I have a dialog pop up in the middle of the program and the user has to
specify between two options then click confirm
problem arises when the user selects nothing then click the confirm button
so i need a way to notice that user hasnt selected anything
and add that check inside confrim button's click listener
im thinking of
....
.setPositiveButton("confirm", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (intent == null)
{ //do nothing or tell the user to select on something }
else
{ getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, i);}}
of course intent == null doesnt work, so what code should I use?
Intent with action name without extras will not be null. if you want to check extras is there with intent then try the following code
if (intent.getExtras() == null) {
// do something if there is not extras.
} else {
}
Related
When I login to my android application for the first time, its works perfectly, after I logout, when I try to login its doesn't open the activity,
I have cleared FragmentStack and saved preferences below.
clearFragmentStack();
clearSavedPrefs();
But When i disable or destroy the application from recent apps or delete data from settings. its working normally.
How can I solve this issue. Please find the code below.
builder.setPositiveButton(android.R.string.ok,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
httpClientService.canCanLogout(new ResponseCallBack<BaseDao>() {
#Override
public void onDataReceived(
HttpResponseHolder<BaseDao> responseHolder) {
if (responseHolder.getErrReason() != null) {
generalProperListener.showShortToastMessage(responseHolder.getErrReason());
} else {
startUserLoginFragment();
ct.event.push("Logout");
}
}
});
}
});
I have cleared the answer.
Where I have concat the previous string value.
//old
UserAddress.ADDR_URL +="?".concat(paramsString);
//new
UserAddress.ADDR_URL =URL+"?".concat(paramsString);
I have a language setting in my android app. I save the current language into SharedPreferences and I have a onSharedPreferencesChanged that restarts all activites in the stack so they can be shown in the correct language.
This works like a charm but the language setting is being shown into a Dialog, so when I click on the language I want to change it to, shared preferences changes and then I get a Leaked Window error.
public void showDialog(View v) {
final CharSequence[] items = { res.getString(R.string.english),
res.getString(R.string.spanish) };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(res.getString(R.string.change_language));
builder.setSingleChoiceItems(items, selected,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
switch (item) {
case 0: // English
if (selected == 0) {
Toast.makeText(
SettingsActivity.this,
res.getString(R.string.current_language),
Toast.LENGTH_LONG).show();
break;
}
changeLocale("en");
break;
case 1: // Spanish
if (selected == 1) {
Toast.makeText(
SettingsActivity.this,
res.getString(R.string.current_language),
Toast.LENGTH_LONG).show();
break;
}
changeLocale("es");
break;
}
alert.dismiss();
}
});
alert = builder.create();
builder.show();
}
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key.equals("language")) {
restartActivity();
}
}
This method is in the parent activity of all of my activities:
public void restartActivity() {
Intent intent = getIntent();
finish();
startActivity(intent);
}
Im getting a leaked window in the line builder.show() because I'm restarting all the activities when the shared preferences change. How can I show the dialog and restart my activities without getting this error?
Thanks in advance
Explaining Abdallah's answer try to move dismiss() up as the first statement in your onClick callback method.
EDIT: First of all after you call alert = builder.create(); you should call alert.show() and not builder.show(). Furthermore having the modifications mentioned in comments you should not receive the error no more.
Your code is trying to show a Dialog after exiting the Activity.
To prevent this, you should dismiss() your Dialog before restarting Activities.
In my Android app I am giving the possibility to tap on a Google +1 button.
This works fine with the following code from the developpers source material (https://developers.google.com/+/mobile/android/recommend):
mPlusClient = new PlusClient.Builder(this, this, this)
.clearScopes()
.build();
mPlusOneButton = (PlusOneButton) findViewById(R.id.plus_one_button);
Now I would like to detect when the user taps on this button, so I tried this:
mPlusOneButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(this, "Thanks for your +1", Toast.LENGTH_LONG).show();
}
});
But it doesn't do anything, it doesn't display the toast message.
As an alternative, I was thinking of having the player tap twice on the button : first being a regular Button object that would make the plus one button visible, second being the plusone button. but it is an awckward user experience
How can I detect user's tap on this button?
Don't use setOnPlusOneClickListener (or any listener) with SDK 13 (and perhaps future versions), it induces a bug (infinite rotation of the progress indicator in the +1 button when you click on it).
Use ActivityForResult to trigger the user choice.
mPlusOneButton.setOnPlusOneClickListener(new PlusOneButton.OnPlusOneClickListener() {
#Override
public void onPlusOneClick(Intent intent) {
startActivityForResult(intent, 0);
}
});
Check the result code
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == -1) {
// OK or SHARE
} else {
//UNDO
}
}
Plus one button provides a listener function to detect button click. Here is the code.
mPlusOneButton.setOnPlusOneClickListener(new PlusOneButton.OnPlusOneClickListener() {
#Override
public void onPlusOneClick(Intent intent) {
Log.w(tag,"plus one click");
}
});
I am using timer(triggers every 2 hours) to check for any update in my app. If there is any update available, it will automatically download the latest .apk and pro grammatically starts the installation activity. (window with OK and CANCEL button)
Now the problem is :
If the user doesn't click "OK" or "CANCEL". Then the popup still exists. So, in the next timer hit (after 2 hours). It is downloading latest .apk, popups another installation activity.so now 2 activity window is displayed. Then for the next timer hit, it will 3 popup activity window (if the user still doesn't chooses OK or CANCEL)
Is there any better way to avoid displaying multiple window.
Please find my code below :
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if (requestCode == 3)
{
isUpdating = false;
}
}
private void fnCheckforUpdate()
{
........
.......
if(false==isUpdating)
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(outputFile), "application/vnd.android.package-archive");
isUpdating = true;
startActivityForResult(intent, 3);
}
}
You can check showing dialog:
if (dialog != null && dialog.isShowing()) {
dialog.dismiss();
}
I need help in geting results back from intent launched from
preference screen
// Intent preference
DevicePref =
getPreferenceManager().createPreferenceScreen(this);
// Show a Screen with list of Devices Discovered
Intent i = new Intent(this,getDevice.class);
DevicePref.setIntent(i);
DevicePref.setTitle("Select Device");
DevicePref.setSummary(mSelectedDevice);
deviceOptionsCat.addPreference(DevicePref);
I want user to select device... In preference screeen I show "Select
Device" .. when user clicks that, another screen is launched by intent
where all devices are listed. User selects the device.
Now how do I know user selected which device? And I want to update
that in the summary.
Pls. let me know
Thanks
I got the answer, Hope it will help someone like me...
Do not mention intent while creating preference like I did in above code.. Mention intent on OnPreferenceClickListener and then do StartActivityForResult()
// Intent preference
DevicePref = getPreferenceManager().createPreferenceScreen(this);
// Show a Screen with list of Devices Discovered
DevicePref.setOnPreferenceClickListener(onPreferenceClick);
DevicePref.setTitle("Select Device");
DevicePref.setSummary(mSelectedDevice);
deviceOptionsCat.addPreference(DevicePref);
Then create OnPreferenceClickListner and here do StartActivityFromResult()
OnPreferenceClickListener onPreferenceClick = new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
if (preference ==DevicePref )
{
Intent i = new Intent(DevuiceOptions.this,getDevice.class);
DevicePref.setIntent(i);
startActivityForResult(i,CHOOSE_DEVICE);
}
return true;
}
};
Finally to get the result handle onActivityResult and update Summary field.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch (requestCode) {
case Constants.CHOOSE_DEVICE:
{
if (data!=null )
{
Bundle b = data.getExtras();
mSelectedDevice = (String) b.get("Name");
UpdatePreferences();
}
}
}
}
Thanks