在Android中关闭对话框后无法触发任何事件

时间:2013-03-08 14:07:34

标签: android dialog

我有一个名为ConvertActivity的活动,它有一个按钮,调用SetPrecisionActivity对话框。显示对话框并且用户按下关闭按钮后,不会调用ConvertActivity的onResume()或onRestart()。我需要在返回ConvertActivity后进行一些处理。

ConvertActivity.java

public class ConvertActivity extends Activity {
private int callingActivity;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.convert_layout);

 } 

public void onClick(View view) {
switch (view.getId()) {
case R.id.setprecision:
    // show the precision screen
    SetPrecisionActivity.app_launched(this);
    break;
}
  }
}

转换布局

  <TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*"  
android:stretchColumns="*"
android:padding="15dp"
android:background="#ffffff">
    <Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_span="1"
    android:gravity="center_horizontal"
    android:drawableTop="@drawable/setprecision"
    android:onClick="onClick"
    android:background="@null"
    android:id="@+id/setprecision"
    android:textSize="12sp"
    android:textColor="#000000"
    android:text="@string/csetprecisionh" />
</TableRow>

setprecisionActivity.java(将显示为对话框)

  public class SetPrecisionActivity {
  private static Spinner spinnerp;

  public static void app_launched(Activity mContext) {
    SharedPreferences prefs = mContext.getSharedPreferences("helloapp", 0);
    SharedPreferences.Editor editor = prefs.edit();
    showPrecisionDialog(mContext, editor);
    editor.commit();
}   

public static void showPrecisionDialog(final Activity mContext, final SharedPreferences.Editor editor) {
    final Dialog dialog = new Dialog(mContext);
    dialog.setTitle("Set precision ");

   // Inflate the popup_layout.xml
   LinearLayout viewGroup = (LinearLayout) mContext.findViewById(R.id.popupprecision);
   LayoutInflater layoutInflater = (LayoutInflater) mContext
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   final View layout = layoutInflater.inflate(R.layout.setprecision_layout, viewGroup);

   // Getting a reference to Close button, and close the popup when clicked.
   Button close = (Button) layout.findViewById(R.id.close);
   close.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    dialog.setContentView(layout);        
    dialog.show();        
}
 }

2 个答案:

答案 0 :(得分:2)

当您打开另一个活动时,您的ConvertActivity不会被暂停或销毁,它只是坐在&#39;那里。查看startActivityForResult方法并等待Result,例如,请参阅here

答案 1 :(得分:0)

要在关闭对话框时触发事件,您需要一个&#34;关于#34;监听器。 它已在此处回答:Can you fire an event when Android Dialog is dismissed?