Is it possible to send a signal from an intent and recover it in another intent

Viewed 56

I have 2 activities.. one is launched via deep links. MainActivity doesn't start DeeplinkActivity. How do I know from MainActivity when DeeplinkActivity finishes.

I have tried setting an intent filter programmatically. then added it to MainActivity then in DeeplinkActivity sendBroadcast.

I was unsuccessful since the broadcast recover method in MainActivity wasn't responding to broadcast sent from Deeplink via sendBroadcast(getIntent())

3 Answers

The function you are looking for is putExtra() put this in your FirstActivity :

Intent intent  = new Intent (FirstActivity.this , SecondActivity.class);
intent.putExtra("your key" , yourBooleanVariable) ;
FirstActivity.this.startActivity(intent) ; 

And this code in Your Second Activity :

Intent intent = getIntent() ;
Bundle bundle = intent.getExtras();
if(intent.hasExtra("your key")){
Boolean boolean = bundle.getBoolean();
}

you can put everything in a bundle with putExtra and get it from the bundle in the second Activity

Both the Model and Broadcast method work.

I chose to use the Broadcast method much simpler

MainActivity

public class MainActivity extends Activity {

    private BroadcastReciever receiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context p1, Intent p2) {
                // TODO: Implement this method
            }
        };
        registerReceiver(receiver, new IntentFilter(getPackageName() + ".FOO_BAR"));
      
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        unregisterReceiver(receiver);
    }
}

DeepLink

public class DeepLink extends Activity {
    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        sendBroadcast(new Intent(getPackageName() + ".FOO_BAR"));
    }
}
Related