sometimes showDialog doesn't work - java

I have a fragmentActivity that use startActivityForResult(intent, getCode) to call an activity that sends some files via ftp.
In the method onActivityResult(int requestCode, int resultCode, Intent data) shows the result of the operation (failure, success, no internet, etc...) using a dialog.
The dialog is called like this:
Bundle bundle = new Bundle();
bundle.putString(LLAVE_TITULO, alertTitulo);
bundle.putString(LLAVE_MENSAJE, alertMensaje);
showDialog(DIALOG_RESPUESTA, bundle);
and the onCreateDialog method is basic
#Override
protected Dialog onCreateDialog(int id, Bundle bundle)
{
Builder mAlertDialog = new AlertDialog.Builder(this);
mAlertDialog.setTitle(bundle.getString(LLAVE_TITULO));
mAlertDialog.setMessage(bundle.getString(LLAVE_MENSAJE));
mAlertDialog.setPositiveButton("Aceptar",null);
return mAlertDialog.create();
}
Everything is working ok, but I noticed when I show and dismiss the dialog really fast (the faster i can, once or twice per second) in some point the dialog doesn't show anymore until I close the activity and start it again...
What is happening here? How can I really be sure that the dialog will show?
Thanks!

activity calls onCreateDialog() one times for each dialog..
so.. I think you should use DialogInterface.OnShowListener for dialog view widgets validation

Related

how to open a webpage, fill an html form, submit it, get the form's result and then post the information to another activity in android

We have an aspx page with a form in it and we can only process this form in a browser-based environment (ie we cannot create a web service for it.
What we want to do is that we want to process this information in an android application. We want to send some initial data to this page, let the user fill it, submit the form, and then return the results to the calling activity, so we can show a proper message and act accordingly.
let me give an example for this usecase:
We are in activity A and in this activity we have a button that triggers the start of our journey:
//we are in activity A
mOurButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putExtra("additional_data_we_need", adittionalData);
startActivityForResult(intent, REQUEST_CODE);
}
});
Now we are in activity B and here, we should comunicate with that webpage that I was talking about, passing it the additionalData that is in this activity's extra bundle (it is acceptable if we do it using query strings or any other way).
After the user fills the form, and hit submit,we do some operation on it and return some results to another (web)page or in the current (web)page's postback (again it is acceptable if we use query strings, post method or any other way; whicever way that works for this senario). We get this returned result in android side and then get the result to ActivityA :
//we are in acitivity B
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
int adittionalData = getIntent().getExtras().getIntExtra("adittional_data_we_nedd");
//this is the first place that I don't know what to do
callTheWebPageAndGiveItTheData(adittionalData);
}
public void SomeListenerOfTheReturnedDataFromTheWebpage(Bundle returnedData //doesn't necessarily need to be of bundle type) {
intent intent = new Intent();
intent.putExtra("return_bundle", returnedData);
setResult(Activity.RESULT_OK, intent);
finish();
}
What can I do to achieve this goal?
Thanks in advance for your kind answers

android switch between activities/layouts

I would like to show loadingActivity while "activity1" is executing some code (working), after that, show again activity1. However, if I do not want to start activity1 again, only switch its layouts when doSomeStuff ends. Thank you.
activity1
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent myIntent = new Intent(getApplicationContext(), loadingActivity.class);
startActivityForResult(myIntent, 0);
//Do some stuff while loadingActivity is showed
doSomeStuff()
//here I want to show again this activity and hide loading one
loadingActivity
public class loadingActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
setContentView(R.layout.loading);
}
For this you should use a ProgressDialog, this will allow you to easily show a loading indicator and once the work is done you can easily remove it. The code below should work of you.
Show Dialog:
ProgressDialog dialog = new ProgressDialog(YourActivityClass.this);
dialog.setMessage("Loading Activity...");
dialog.show();
//Do your long running work here
Dismiss dialog:
dialog.dismiss();
You can set the dialog as a class level variable if you want to show and dismiss it in different methods.
Also from looking at your code you might be blocking the activity from ever loading if your long running work is not happening on a background thread. You cannot do long running work inside of onCreate without offloading the work to a background thread. For easy threading in Android you should use the AsyncTask class.

Android - After closing an activity, when I run the app again, two activities run at the same time. How can I avoid it?

So I've got an activity in my android app, that runs on start.
This activity is just a page with a start button.
When I press the start button, it calls another activity and closes itself:
Intent i = new Intent(this, Dictating.class);
startActivity(i);
finish();
The other Activity is using Text-to-speech to dictate some words.
Now I've got something weird happening:
1) I listen to the dictating.
2) I press back button: dictating stops (what I want)
3) I run again the app, press the start button. Now I have my new activity running and dictating, but in the back I can hear the older Activity that resumed where it was, and continues dictating.
I would like for the new activity to start all over again, and not keep the other activity.
How can I do that ?
PS: This is an activity problem, and not a text-to-speech problem as I'm flushing the text-to-speech each time, It could not be kept in the memory
Thank you
EDIT:
Here is the onCreate of my Dictating class, there is tons of code in this class, I obviously don't want to post all my code, so here is some parts:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.streaming);
Intent checkIntent = new Intent();
checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);
this.txtCurrentWord = (TextView) findViewById(R.id.txtCurrentWord);
this.btnPlayPause = findViewById(R.id.btnPlayPause);
this.btnPlayPause.setOnClickListener(this);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
// success, create the TTS instance
this.tts = new TextToSpeech(this, this);
} else {
// missing data, install it
Intent installIntent = new Intent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
there are a few weird things I'm doing like:
Runnable task = new Runnable() {
public void run() {
//runs on ui
runOnUiThread(new Runnable() {
public void run() {
readNextWord();
}
});
}
};
worker.schedule(task, 1, TimeUnit.SECONDS);
this delays the next word by one second, and then executes a fonction in the main ui thread. not sure if this matter
And some flushing at the end:
#Override
public void onDestroy() {
tts.shutdown();
super.onDestroy();
}
You need to add launchMode property to your activity inside AndroidManifest file, for more detail see "Using the manifest file"
This question is over a year old, but I can't believe no one ever gave you the right answer. Also, I don't think you should have accepted an answer that clearly didn't solve anything for you. By accepting such answers, you're just cluttering the StackOverflow google search results with junk for other people with the same problem.
The flushing you do at the end is completely wrong. According to the Activity lifecycle, onDestroy() is never guaranteed to be called. If you want to make sure the flushing gets done properly, do it inside of onPause().
For now the solution I'm giving you does fix the main problem you've described. However, if you do get the time to do a more complete rewrite, you'll want use a service that you bind to your activity. That will give you the finer control you require.

Android - Close all activities (FLAG_ACTIVITY_CLEAR_TOP/clearTaskOnLaunch didn't help)

I have "MainMenuActivity" in my application, from which I want to log out. After pressing the back button, this activity should start "Logout activity", which does some logout stuff and then finishes the application.
Method called onBackPressed() from MainMenuActivity:
public static void logoutAction(final AbstractActivity activity) {
Builder dialog = new AlertDialog.Builder(activity);
dialog.setPositiveButton(R.string.dialog_btn_yes, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent();
i.setClass(activity, iess.student.login.LogoutActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
activity.startActivity(i);
activity.finish();
}
});
dialog.show();
}
Then, "LogoutActivity" executes AsyncTask, which at the end of its work calls finish() on LogoutActivity.
My problem is, that if other activities were launched before MainMenuActivity, i.e. A -> B -> MainMenuActivity, then after pressing back button Logout activity does its work, finishes, but instead of closing the application, activity B comes to front. I tried to launch MainMenuActivity from activity B with FLAG_ACTIVITY_CLEAR_TOP and then call finish() on B, but in that case A came to front. I also tried to set:
<activity android:name="abc.def.LogoutActivity" android:clearTaskOnLaunch="true"></activity>
But the result was the same as before. Could you please help me what should I do?
OK, so I finally managed it. After creating an activity, I register it in static ArrayList<Activity>. After "LogoutActivity" does its work; it just calls finish() on each Activity registered in ArrayList. It works, but I guess it's not really nice. But I haven't figured out how to do this with FLAG_ACTIVITY_CLEAR_TOP.
As #sandrstar says in the comment:
from API 11 you can use FLAG_ACTIVITY_CLEAR_TASK
It works very well for me, as it removes all Activities, not just the ones on top.

Andriond send event from first activity and receive on the second

I have two activities. The first activity display list of the users with short info. And after select some user I go to the second activity for display full info about this user. For send event I used startActivityForResult(); for receive event in socond activity and added public void onActivityResult(int requestCode, int resultCode, Intent data). After start project I send intend from first activity and I do not receive in the second :(. How I can receive sent event in second activity?
Thank you...
You implement onActivityResult in the first activity in order to get the result from the sub-activity you've started. In your sub-activity you receive the event in the onCreate method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Intent intent = getIntent();
// Do some setup based on the action being performed.
final String action = intent.getAction();
if (action.equals()) {
}
}
I recommend that you have a look at the Notepad sample to see how things work. startActivityForResult may not be needed - startActivity should be enough. You usually don't return something from the item view to the list view.

Categories

Resources