xRay
xRay

Reputation: 801

Start activity but use the same instance

In my MainActivity, a dialog can be opened. When there are changes in that dialog, a value will be stored in SharedPreferences and then this should happen in MainActivity: findViewById(R.id.icon_dialog).setVisibility(View.VISIBLE);

I was not able to change the view of MainActivity when the dialog was still opened, so I decided to start the activity, like this:

Intent intent = new Intent(getContext(), MainActivity.class);
requireContext().startActivity(intent);

This is just what I wanted and the view of MainActivity will be changed. But my problem is dialogClass. Since the dialog restarts the MainActivity class, dialogClass will be initialized again. So I am never able to stop the same service.

So how can I restart MainActivity but don't initialize dialogClass again?

public class MainActivity extends AppCompatActivity implements View.OnClickListener, Popup.ICustomTts, Popup.ITarget {
    Dialog dialogClass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (dialogClass == null) {
            dialogClass = new Dialog();
        }

        // Activate / deactivate dialog
        findViewById(R.id.icon_dialog).setVisibility(View.GONE);
        if (Helper.getSharedPreference(getApplicationContext(), "dialog") != null && Helper.getSharedPreference(getApplicationContext(), "dialog").equals("1")) {
            dialogClass.startService(getApplicationContext());
            findViewById(R.id.icon_dialog).setVisibility(View.VISIBLE);
        }
        else {
            dialogClass.stopTheService(getApplicationContext());
        }

Upvotes: 0

Views: 48

Answers (1)

Tyler V
Tyler V

Reputation: 10940

There is no way to preserve an instance of an Activity to re-use for a later launch - the application lifecycle handles creating and destroying those.

However, if you just want an action in the dialog to change the visibility of a view in the activity that launched it, you can do that with a callback instead of trying to re-launch the activity.

First, add a callback interface in your dialog class

public class MyDialog {

    // The interface can define actions you want your activity
    // to handle (can include more than one method here, or add
    // arguments to the methods as needed)
    public interface OnSelection {
        void showView();
    }
    
    OnSelection callback = null;
    
    void someFunction() {
        // add these lines in the dialog where you want to show the view
        if( callback != null ) {
            callback.showView();
        }
    }
}

Then implement the callback interface in your main activity and set it on the dialog instance in onCreate

public class MainActivity extends AppCompatActivity implements 
    View.OnClickListener, 
    Popup.ICustomTts, 
    Popup.ITarget, 
    MyDialog.OnSelection // add to the list of interfaces it implements
{
    // Probably safe to just instantiate the Dialog here
    final Dialog dialogClass = new Dialog();

    // the dialog will call this method via its callback
    @Override
    public void showView() {
        View icon = findViewById(R.id.icon_dialog);
        if( icon != null ) {
            icon.setVisibility(View.VISIBLE);
        }
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // MainActivity implements the callback interface, so we set
        // "this" (a.k.a. MainActivity) as the callback.
        dialogClass.callback = this;
        
        // other stuff
    }
}

You could also have the dialog take the callback as a constructor parameter and pass in this when you construct it rather than setting the callback later.

Upvotes: 1

Related Questions