I’m getting really confused with dialog boxes in Android and need some advice.
Everything was initially going well. I had numerous dialogs being created within MainActivity opened via a Navi Drawer. The dialogs where created very simply using code like this:
private void exportDialog() {
LayoutInflater inflater = this.getLayoutInflater();
final View formElementsView = inflater.inflate(R.layout.export_data, null, false);
AlertDialog msgBox = new AlertDialog.Builder(this)
.setView(formElementsView).setTitle("Export Responses")
.setIcon(android.R.drawable.ic_menu_share)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Code...
}
})
.create();
msgBox.show();
}
But I started to notice problems when the device resumed from lock screen. Switching/pausing/resuming worked fine, but the locked screen seemed to kill the activity context and when the app resumed the dialogs became graphically corrupt and I got the “Activity has leaked window” error message in the logs.
So, I started again. I moved all the dialogs into DialogFragment classes which seems to be the ‘proper’ way to do it. This removes the error and graphical glitches. Great!
But I can’t work out how to add more than one dialog listener to MainActivity. So I’ve got:
public class MainActivity extends Activity implements LogInDialog.NoticeDialogListener{
but can I add more?
Until I work out how to do this, I’ve altered many of the MainActivity methods and moved them into the relevant dialog classes, which works fine but seems a rather linear approach. It would be nice if MainActivity could act upon each dialog response.
As you can guess, I’m not a professional developer so I’m getting rather lost!
UPDATE:
OK, it seems the original problem of graphic glitches and "leaked window" messages was because the dialog boxes were not being dismissed correctly. Adding the following seems to have greatly improved matters:
#Override
public void onDestroy() {
super.onDestroy();
if (DialogBox1!=null){
DialogBox1.dismiss();
}
DialogBox1= null;
if (DialogBox2!=null){
DialogBox2.dismiss();
}
DialogBox2= null;
if (DialogBox3!=null){
DialogBox3.dismiss();
}
DialogBox3= null;
...
use inner classes that implements LogInDialog.NoticeDialogListener
Related
I am new to android development and stuff. Working on an existing android application and tried to use progressDialogue the spinner one.
I have declared the progressdialog kinda globally. showing and dismissing them in separate methods both of which run on ui thread.
private void progressShow()
{
runOnUiThread(new Runnable() {
#Override
public void run() {
//progress_layout.setVisibility(View.VISIBLE);
progressDialog = new ProgressDialog(LoginActivity.this,R.style.Theme_IAPTheme);
progressDialog.setIndeterminate(true);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage("Authenticating...");
progressDialog.show();
login_button.setEnabled(false);
register_button.setEnabled(false);
user_name_edit_text.setEnabled(false);
}
});
}
The code above is working fine but the style of the progressdialog is very dumb. When I don't mention R.style.Theme_IAPTheme the style is a good one but there is no spinner there.
How would I get some other style?(I am learning from this link:http://sourcey.com/beautiful-android-login-and-signup-screens-with-material-design/). This guy used R.style.AppTheme_Dark_Dialog which becomes red for me when I use it. :DDDD
One of the great things about android is availability of so many open source libraries. If you're not satisfied by the style of progress dialog you can use a custom one. Refer to this URL, a list of few fancy progress dialogs you might like.
Are transitions turned off in developer options on the device.
This can cause certain animations from the system to not display. Unfortunately, that includes the progress dialog spinner.
In my first steps in exploring Android I now start with QR scanning.
Works all pretty well. But I am not able to come back from the ResultHandler after read the QR successfully to my MainActivity.
public class MainActivity extends AppCompatActivity implements
ZXingScannerView.ResultHandler
{
private ZXingScannerView mScannerView
....
#Override
public void handleResult(Result rawResult)
{
// my results are ok in rawResult
// the scanner does not scan anymore but it is still there
// how to go back to my main activity???
}
public void ClickButton (View view)
{
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}
}
}
I tried
mScannerview.stopCameraPreview
mScannerView.stopCamera
this.finish
setContentView(R.layout.activity_main); // shows my activity_main
// but I can not click anything
Thanks!!
EDIT
I added some code to describe it a bit better. The idea is from
https://www.numetriclabz.com/android-qr-code-scanner-using-zxingscanner-library-tutorial/
Your question isn't clear but I'm assuming you want to restart the scan process. Normally, you'd have to restart the SurfaceHolder to be in preview mode. Luckily for you the ZXingScannerView already has a method to do that. Call mScannerView.resumeCameraPreview(this) to restart the scan process.
Otherwise can you clarify? You say you want to go back but you're already in MainActivity
If you want to go back into activities/fragments stack you can try Activity.onBackPressed()
if you are in a fragment you must call this method against attached Activity
What do you want is not going back to your activity. You want to restore activity's layout.
I think the better choice is to add ScannerView to your activity's layout file with android:visibility="gone". Then in on click you can get this view and change it's visibility to VISIBILE.
Then when you have handled scanning result, you can reset yuoir ScannerView to visibility = GONE
I too was stuck with this problem for an hour, just like you. And later realised..
To solve this problem DON NOT implementing the ZXingScannerView in the same activity or fragment. Instead start a new activity when you click the button and this activity is just for the ZXingScannerView
Once the Scan is done finish and pass the data back to your activity or fragment
Just restart your MainActivity before this.finish()
the code below will start your main activity through intent...
worked fine for me
startActivity(new Intent(this,MainActivity.class));
this.finish();
remove from onCreate method this line setContentView(your layout) and when you finish scan write it after you stoped the camera then you can use your layout after scan
I had a bit of a look into android concepts and activities.
I put the QR handling in a 2nd activity and it worked well with the finish ().
Thanks for help anyway!!
I think it's too late, but I came with the same problem and I had so find a solution by myself.
You were on the right way, you need two steps more.
I called the methods where I link and set the listener of any buttons
There are the methods
Basically you were right where you set the content view, but you need to give the buttons their functionality back.
(I know its late, but better late than never). Good luck!
I'm trying to figure out if the map is loaded before I open a dialog on the MainActivity (with map fragment) it would be helpful if there was a
if(map.isLoaded()){
//show dialog
}
if I just normally open a dialog after the map is already loaded, then it opens fine, but I'm switching activities and I wanted the dialog to open as soon as the activity starts but it seems as if the dialog is beating the loading map, then through the process of it loading, the dialog goes away and the map comes up. I've seen things about javascript and theres something regarding tilesLoaded() but I'm not seeing anything like that either. Any suggestions??
How about GoogleMap.OnMapLoadedCallback?
private GoogleMap mMap;
mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
public void onMapLoaded() {
//do stuff here
}
});
Hey guys, i am making an android application where i want to show a dialog box about legal agreement everytime the application starts, i have a public method showalert(<>); which shows an alertdialog by building a dialog with alertbuilder. I added a call to showalert() method on the onCreate() method of the main activity to show it, but whenever the user rotates the screen, he gets the dialog everytime. The activity restarts itself when the phone is rotated. I tried adding android:configChanges="keyboardHidden|orientation" to my manifest but that doesnt help on this case. Also can i know how to register a new application class on manifest file. I am trying to create an application class and put the code to show dialog on the new class's oncreate method. But i am not being able to load the class when the app starts.
I also checked Activity restart on rotation Android but i dont seem to get a thing. I am pretty much a newbie to android programming, could someone simplify that for me?
Any help would be appreciated. :)
you could maybe look at the onRetainNonConfigurationInstance() activity method, which is called just before destroying and re-creating the activity on screen orientation change.
it allows you to retain an object that could for instance contain a test variable to know if your legal thing was already shown or not.. example :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String test = (String) getLastNonConfigurationInstance();
if (!("textAlreadyShown").equals(test)) {
//here : show your dialog
}
}
#Override
public String onRetainNonConfigurationInstance() {
return "textAlreadyShown";
}
Set the main activity to an activity that just shows the legal notice, when it is accepted/cleared, show a second activity ( which is currently the main activity )?
I have a really weird problem, which I am unable to debug so far...
Thing is...my app needs to download something to work. So in the beginning of the onCreate() method, I check if that something is already downloaded. If not, I pop a dialog up asking the user to download it.
if (!isInstalled) {
showDialog(DIALOG_INSTALL);
} else {
start();
}
Where start() method performs some other action. Now, that showDialog calls this:
builder = new AlertDialog.Builder(MyApp.this);
builder.setMessage("Would you like to install...")
.setCancelable(false)
.setPositiveButton("Install", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
aManager.install(MyApp.this);
}
});
dialog = builder.create();
return dialog;
My dialog is shown and I am clicking, so aManager.install() is called. I am passing the context because that aManager.install() pops up a ProgressDialog to show downloading progress and spawns a new thread in which everything is downloaded. So obviously before creating my dialog I make a Handler to receive the response from that aManager.install(). And the response MAY vary, because for example the internet connection isn't available (Exception raised and catched and listener called with different code).
Now, when that happens (Exception) I would like to call another dialog saying "something went wrong, would you like to retry?"...so another call to showDialog(DIALOG_REINSTALL) (this time with another code).
Thing is...the showDialog() gets called (I can verify this by logging) but the dialogs doesn't show up. Instead my application JUST HANGS!?!?!?
Does someone have a clue why it's doing this???? No exception raised, absolutely nothing from logcat, I can't tell WHERE it's hanging...just see that the method is called and the dialog should be displayed...
Thank you very much!!
Looks like you have a deadlock. I would put the download code on the separate thread e.g. use AsyncTask. In task.onPreExecute() you can dismiss 1st dialog and pop-up your progress dialog which you update by overwriting task.onProgressUpdate()
Use .show() instead of .create().