I have a custom dialog activity class, which I call when I have to show a waiting bar for a particular process, after that I call it's finish method but I am unable to finish that activity there or I don't know how to call that finish method, yet I am calling it via class's object my code for WaitDialogManager
class is below. And I don't want to use Broadcast receiver for that...
WaitDialogManager
package com.android.remotewipedata;
import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.widget.TextView;
public class WaitDialogManager extends Activity {
TextView waitTitle, waitMessage;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.wait_dialog);
String title = getIntent().getStringExtra("waitDialogTitle");
String message = getIntent().getStringExtra("waitDialogMessage");
waitTitle = (TextView) findViewById(R.id.wait_dialog_title);
waitMessage = (TextView) findViewById(R.id.wait_dialog_message);
waitTitle.setText(title);
waitMessage.setText(message);
}
public void dismissWaitDialog(){
this.finish();
System.out.println("Finish Called");
}
}
and this is where I am calling this activity and trying to finish it after completion of the method, code for that non-activity class is below
ServerUtilities
public final class ServerUtilities {
//Other code
public static WaitDialogManager wdManager = new WaitDialogManager();
static boolean register(final Context context, String name, String email,
final String regId) {
// Starting WaitDialogManager activity here
context.startActivity(new Intent(context, WaitDialogManager.class)
.putExtra("waitDialogTitle", "Please wait...")
.putExtra("waitDialogMessage",
"Registering device on Server...")
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
String serverUrl = SERVER_URL + "/register.php";
Map<String, String> params = new HashMap<String, String>();
params.put("regId", regId);
params.put("name", name);
params.put("email", email);
// Try to register on server for a number of times
long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000);
for (int i = 1; i <= MAX_ATTEMPTS; i++) {
try {
post(serverUrl, params);
System.out.println("Parameters: " + params);
GCMRegistrar.setRegisteredOnServer(context, true);
return true;
} catch (IOException e) {
if (i == MAX_ATTEMPTS) {
break;
}
try {
Thread.sleep(backoff);
} catch (InterruptedException e1) {
Thread.currentThread().interrupt();
return false;
}
backoff *= 2;
}
}
wdManager.dismissWaitDialog();
return false;
}
for this dialog to disappear I have manually click the back button so that it disappear, I want it to dismiss/disappear when register()
method reaches it's end. Thanks
In my experience you technically can't do this. What you can do though is start the second activity with startActivityForResult
and then when your second activity finishes you can pass back a flag that indicates that the calling activity should exit. This process will be so fast that the user will not be able to tell that the calling activity was still open. If the first / calling activity should always exit then in the manifest you can set android:noHistory="true"
for the activity.
I solved it by taking a static reference of Current activity from WaitDialogManager
class as below. In WaitDialogManager
class declare static Activity
reference,
public class WaitDialogManager extends Activity {
public static Activity context = null; // Current Activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.wait_dialog);
....
}
}
And calling it in ServerUtilities
like blow,
public final class ServerUtilities {
//Other code
static boolean register(final Context context, String name, String email,
final String regId) {
// Starting WaitDialogManager activity here
context.startActivity(new Intent(context, WaitDialogManager.class)
.putExtra("waitDialogTitle", "Please wait...")
.putExtra("waitDialogMessage",
"Registering device on Server...")
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
String serverUrl = SERVER_URL + "/register.php";
Map<String, String> params = new HashMap<String, String>();
params.put("regId", regId);
params.put("name", name);
params.put("email", email);
// Try to register on server for a number of times
long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000);
for (int i = 1; i <= MAX_ATTEMPTS; i++) {
try {
post(serverUrl, params);
System.out.println("Parameters: " + params);
GCMRegistrar.setRegisteredOnServer(context, true);
WaitDialogManager.context.finish(); // And this is how it works
return true;
} catch (IOException e) {
// Exception handled
}
}
return false;
}
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.