-1

I have simple app with 2 activities. One of them is transparent. All process looks like :

1) I lunch my app, at start displayed is my MainActivity

2) I click button "show transparent activity" and after that displayed is my TransparentActivity (under it still a little bit visible is my MainActivity)

3) I click button "Close transparent Activity" after that my TransparentActivity is closed(killed/destroyed) and after that I create and display Dialog (above my MainActivity).

I have problem with last point (3) how I should build and display this dialog exactly after close TransparentActivity ? How I should close this Transparent app ? should I just finish() it or maybe build intent to MainActivity ?

1 Answers1

2

You should use startActivityForResult method.

For your particular case you could implement something like this:

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private static int REQUEST_CODE = 1234;

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

        final Button btnShowTransparencyActivity = (Button) findViewById(R.id.button);
        btnShowTransparencyActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {
                final Intent intent = new Intent(MainActivity.this, TransparentActivity.class);
                startActivityForResult(intent, REQUEST_CODE);
            }
        });
    }

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, Intent intent) {
        if (REQUEST_CODE == requestCode) {
            if (resultCode == Activity.RESULT_OK) {
                Toast.makeText(this, "Show dialog", Toast.LENGTH_SHORT).show();
            } else if (resultCode == Activity.RESULT_CANCELED) {
                Toast.makeText(this, "Don't show dialog", Toast.LENGTH_SHORT).show();
            }
        }
    }

}

TransparentActivity.java

public class TransparentActivity extends AppCompatActivity {

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

        final Button btnCloseTransparentActivity = (Button) findViewById(R.id.button);
        btnCloseTransparentActivity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View view) {
                final Intent intent = new Intent();
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });
    }

    @Override
    public void onBackPressed() {
        final Intent intent = new Intent();
        setResult(Activity.RESULT_CANCELED, intent);
        super.onBackPressed();
    }
}
AlexTa
  • 5,133
  • 3
  • 29
  • 46
  • but what result_code should I use to get closed activity ? –  May 12 '17 at 09:06
  • You can use `RESULT_OK` or `RESULT_CANCELED` https://developer.android.com/training/basics/intents/result.html Also when TransparentActivity is launched MainActivity lifecycle will change to `onPause` and when TransparentActivity is finished the `onResume` of MainActivity will be called. – sagix May 12 '17 at 09:08
  • 2
    Do you follow the link I have provided to you? It is pretty clear what you have to do – AlexTa May 12 '17 at 09:11
  • Yea, I'm struggling with it for a while and I used lifecycle methods problem is that all it looks like : When transparentAvtivity is shown my MainActivity goes to onPause when I finish TransparentActivity called is onResume for my MainActivity and after that called is onDestroy for TransparetAvtivity, but i try to do that with StartActivityforResult –  May 12 '17 at 09:22
  • So I should setResult(RESULT_CANCELED) while closing Transparent and inside Main i need to build onActivityResult with something like if (requestCode == RESULT_CANCELED) { //Create dialog ? } –  May 12 '17 at 09:33
  • @AlexTa all this works good but still I get problem while I'm trying to display dialog inside if (resultCode == Activity.RESULT_OK) { ///display dialog } Error : Failure delivering result ResultInfo{who=null, request=1234, result=-1, data=Intent { }} to activity {com.example.kylu.layout/com.example.kylu.layout.GuidePhotoAlbum}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState Maybe you have cure for this too ? –  May 12 '17 at 10:18
  • @Fuk That's another question, create a new question and post the code so we can understand what is happening – AlexTa May 12 '17 at 10:23
  • @AlexTa and here we are (http://stackoverflow.com/questions/43935681/failure-delivering-result-resultinfo-java-lang-illegalstateexception-can-not) –  May 12 '17 at 10:37