'Is it possible to send a signal from an intent and recover it in another intent
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())
Solution 1:[1]
You can use interface, inside onDestroy() of DeepLink activity call the interface method and implement the method in MainActivity. Check this answer. https://stackoverflow.com/a/19027202/7248394
Solution 2:[2]
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
Solution 3:[3]
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"));
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ankit |
| Solution 2 | Amirhussein |
| Solution 3 | Bret Joseph |
