I have non-activity class MenuHandler
to handle menu-related events, I am trying to display developers message in alert dialog box, this message fetch from firebase
real time database.
Everything is fine but alert dialog not displaying, I try debugger to check there any error in database but I got value properly from database. No Error to get value from database.
when I select menu from MainActivity
, developerMessage
toast shows then nothing happened.
I pass context
to MenuHandler
class properly. Can I Show Alerdialog
in using context? or I need to write code only in MainActivity
only(ie only activity class).
Code:
public void developersMessage()
{
if (isInternetOn()) {
Toast.makeText(mContext,"Loading message please wait",Toast.LENGTH_SHORT).show();
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.child("version_1_0").child("dev").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
try {
final AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.AppCompatAlertDialogStyle);
builder.setTitle(mContext.getString(R.string.welcome_msg));
builder.setMessage(dataSnapshot.child("dev_msg").getValue(String.class));
builder.setPositiveButton("ok",null);
builder.create().show();
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
else
{
Toast.makeText(mContext,"please turn on internet ",Toast.LENGTH_SHORT).show();
}
}
Build your AlertDialog out of
mDatabase.child("version_1_0").child("dev").addListenerForSingleValueEvent(...);
The addListenerForSingleValueEvent(...)
method runs on a different background thread, so the UI can't be updated inside off UI thread. Thus building your AlertDailog out of the addListenerForSingleValueEvent(...)
method should solve your problem.
You should use activity context to create AlertDialog.
And please debug to make sure onDataChange()
is called
Also try with runOnUiThread: https://developer.android.com/reference/android/app/Activity.html#runOnUiThread(java.lang.Runnable)
yourActivity.runOnUiThread(new Runnable() { public void run() {... code to create & show dialog ...} })
I got the answer, There nothing wrong in the code but bug lies in how I send context from mainactivity
here I wrongly init Menuhandler class
public boolean onOptionsItemSelected(MenuItem item) {
final MenuHandler mMenuHandler = new MenuHandler(this.getApplicationContext());//bug lies here
if we are showing alertdialog then we need this way to pass context so our alertdialog display properly
public boolean onOptionsItemSelected(MenuItem item) {
final MenuHandler mMenuHandler = new MenuHandler(MainActivity.this);//this is right way to pass context
hope this is helpful to someone in future.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.