0

I have an activity called ConvertActivity that has a button, which calls SetPrecisionActivity dialog. After the dialog is displayed and the user presses the close button, the onResume() or onRestart() of ConvertActivity is not been called. I need to do some processing after returning to the 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;
}
  }
}

the convert layout

  <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>

the setprecisionActivity.java (which will be shown as a dialog box)

  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();        
}
 }
David Gras
  • 958
  • 2
  • 12
  • 21
user1848880
  • 77
  • 10

2 Answers2

2

Your ConvertActivity is not paused or destroyed when you open another Activity, it just 'sits' there. Look at the startActivityForResult method and wait for the Result, see here for example.

Community
  • 1
  • 1
fweigl
  • 21,278
  • 20
  • 114
  • 205
  • I agree that the ConvertActivity just sits there, but I would have thought that it would be paused or something while the dialog is active and when we close the dialog, it should have resumed or restarted something.. – user1848880 Mar 08 '13 at 14:35
  • One might think. It definitely isn't destroyed, might be paused but you aren't guaranteed. Have a closer look at the Activity lifecycle docs. Anyway, the usual way when you want to wait for another Activity to finish, then do something is the way I described above. – fweigl Mar 08 '13 at 14:42
  • True, I have changed my code as you have mentioned to get the necessary results and it worked.. thanks a lot. – user1848880 Mar 08 '13 at 16:02
  • This solution will only work if you re-implement the dialog as an Activity. – David Gras Sep 11 '14 at 10:34
0

To trigger an event when a dialog is clossed you need an "on dismiss" listener. It's already answered here: Can you fire an event when Android Dialog is dismissed?

Community
  • 1
  • 1
David Gras
  • 958
  • 2
  • 12
  • 21