我在使用Theme.Dialog完成活动时遇到问题..需要双按后退按钮才能在返回上一个活动之前完成活动。一按后退按钮=隐藏激活(Theme.Dialog),然后再按返回上一个活动。
和此代码Acitivity(Theme.Dialog)
package com.tracking.bus.track;
import com.tracking.bus.R;
import org.holoeverywhere.app.Activity;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.content.Intent;
import org.holoeverywhere.app.Dialog;
public class language extends Activity {
private Ringtone r;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if(alert == null){
// alert is null, using backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// I can't see this ever being null (as always have a default notification)
// but just incase
if(alert == null) {
// alert backup is null, using 2nd backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
Dialog dialog = new Dialog(language.this);
dialog.setContentView(R.layout.dialog_notifikasi);
dialog.setTitle("Raygional");
dialog.show();
r = RingtoneManager.getRingtone(language.this, alert);
r.play();
}
@Override
public void onStop() {
super.onStop();
if(r.isPlaying())
{
r.stop();
}
finish();
}
}
那么,当隐藏/关闭活动时如何使用Theme.Dialog完成活动?对不起我的英文
答案 0 :(得分:0)
如果我正确理解您的问题,您希望在用户第一次点击"返回"时,调用活动的finish()方法,是吗?在您的活动中:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
this.finish();
}
return super.onKeyDown(keyCode, event);
}
答案 1 :(得分:0)
添加OnDismissListener。
package com.tracking.bus.track;
import com.tracking.bus.R;
import org.holoeverywhere.app.Activity;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.content.Intent;
import org.holoeverywhere.app.Dialog;
public class language extends Activity {
private Ringtone r;
private DialogInterface.OnDismissListener mOnDismiss; // Variable OnDismiss
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
mOnDismiss = new DialogInterface.OnDismissListener(){ // initializing variable
@Override
public void onDismiss(DialogInterface dialog) {
finish();
}
}
if(alert == null){
// alert is null, using backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// I can't see this ever being null (as always have a default notification)
// but just incase
if(alert == null) {
// alert backup is null, using 2nd backup
alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
}
}
Dialog dialog = new Dialog(language.this);
dialog.setContentView(R.layout.dialog_notifikasi);
dialog.setTitle("Raygional");
dialog.setOnDismissListener(mOnDismiss); // assign variable.
dialog.show();
r = RingtoneManager.getRingtone(language.this, alert);
r.play();
}
@Override
public void onStop() {
super.onStop();
if(r.isPlaying())
{
r.stop();
}
finish();
}
}