In my MainActivity
I have 5 buttons, 4 of wich open a WebViewActivity
to display static HTML-content. The 5th button goes to PumpsActivity
which contains additional 5 buttons that also open the WebViewActivity
to display static HTML-pages.
I'd like to be able to "navigate up" from the WebViewActivity
to the appropriate parent activity. For instance: if the WebViewActivity
was called from MainActivity
than navigating up should take the user to MainActivity
. This is easy implemented following the guide on android.developer.com.
But how do I get the WebViewActivity
to navigate up to the PumpsActivity
? How can I check from which activity the WebViewActivity
was started?
Relevant code from my WebViewActivity
(basically the same as in the guide):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent intent = getIntent();
setTitle(intent.getStringExtra("WEBVIEW_TITLE"));
webview = (WebView) findViewById(R.id.wvMyWebView);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webview.loadUrl(intent.getStringExtra("WEBVIEW_URL"));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This is called when the Home (Up) button is pressed
// in the Action Bar.
Intent parentActivityIntent = new Intent(this, MainActivity.class);
parentActivityIntent.addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(parentActivityIntent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
As Leonidos suggests, you could add a parameter, but this introduces extra coupling, and I get the feeling that the parents only have the Activity superclass in common, and passing an Activity type isn't ideal.
One way that springs to mind to do it is to have another String extra in WebViewActivity which indicates the calling Activity. In onOptionsItemSelected you could then check the parent string extra to see which Activity called it, and navigate to that Activity as appropriate.