Reputation: 8549
I used the following function to come back to Home page(Home Activity) from any child.
My task is if we are in any child page it should come to home page and want to display an alert box.
Its working fine for all child pages without the alert dialog.
Its working for following case with alert dialog
For ex: HomePage -> child A
Its coming to home page and showing the alert dialog
Its not working for if i'm in more than one child pages
For ex: HomePage -> child A -> Child B
Its coming to HomePage and became unfocussed, but the alert dialog is not showing.
According to my task, the alert should be cancellabe=false, so cant able to go back with out clicking button in alert dialog
public void ShowConnectivityMessage()
{
Intent intent = new Intent( this, Homepage.class );
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.startActivity( intent );
new AlertDialog.Builder(this)
.setTitle("Cannot Connect To Service")
.setMessage("The service has been unreachable for 1 hour. ")
.setCancelable(false)
.setPositiveButton("Help me shut down the app", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
if(Homepage.bIsLoggedInM)
{
StartTimer();
ShowHowToQuitTheApp();
}
}
}).show();
}
So please any one help me.................
Upvotes: 0
Views: 1890
Reputation: 42155
On the CLEAR_TOP Intent, you should also set an extra that indicates the dialog will be displayed:
public static void returnToHomepage(Context context) {
Intent intent = new Intent( context, Homepage.class );
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// This is new:
intent.putExtra("isNetFail", true)
context.startActivity( intent );
}
Then in Homepage#onResume(), if that extra is defined, then use showDialog() to build and display the dialog. Doing this in onResume()
instead of instantiating and displaying the dialog manually allows you to follow the proper Android Activity Lifecycle -- e.g., managing the dialog within the Activity (as it was designed), and it ensures the Homepage activity is coming back into the foreground at the time the dialog is displayed, thereby avoiding any leaking views or windows.
protected void onResume() {
// ...
if (getIntent().getBooleanExtra("isNetFail", false)) {
showDialog(DIALOG_NETWORK_FAIL);
}
}
Then when you want to return to the Homepage activity (from whatever NChild is active), you would call:
Homepage.returnToHomepage(this);
Finally, you shouldn't do the 30-second polling via Handler in your Homepage activity, because once the Homepage activity is paused/stopped, you no longer have any guarantee that it will stick around. See Multitasking the Android Way. Instead, I would recommend creating a common abstract MyCommonActivity
class, which all your NChildActivity would extend. That activity can start a thread to do the network polling. But you need to stop it in onPause()
, and resume it in onResume()
. What you're doing currently, implies that Homepage is doing work while it's in the background, and that's a huge no-no, for several reasons. In fact, a more appropriate approach would be to setup a background service that is responsible for checking network connectivity, or use a BroadcastReceiver to listen for network connectivity changes.
Upvotes: 1
Reputation: 16110
What i am suggesting is that you call the alert dialog from the homepage activity not the child activities. this way it will work correctly and secondly you dont have to make the same dialog code in each of your childs ( by doing this you create a maintenance nightmare, a single chage to the dialog and you will need to go through all of the screens to change it..).
Upvotes: 1
Reputation: 4446
for every child you have to write method for android back button and there set to Homepage so that from every child you can easily move to Homepage.
Upvotes: 1