Question

Hello all I have implemented a back button in the action bar in my app but I dont know how to return from an activity to a fragment or a fragment to fragment. so if someone could show me how to return from an activity to a fragment or even a fragment to another fragment that would be amazing. Here is the code i have right now

public class Article extends Activity{
private WebView webview;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.articleview);
    // etc...
    getActionBar().setDisplayHomeAsUpEnabled(true);




    Bundle b = getIntent().getExtras();
    String KEY_LINK = b.getString("b");
    String url = getIntent().getStringExtra("key");

    WebView myWebView = (WebView) findViewById(R.id.webView);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    myWebView.loadUrl(url);

    myWebView.loadUrl(KEY_LINK);


    }


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        default:
            return super.onOptionsItemSelected(item);

}
        }

        }

and in my manifest I have

 <activity
        android:name=".Article"

        android:label="@string/app_name"
        android:screenOrientation="portrait">
        <intent-filter>

            <action android:name="com.NTCI.graffiti.Article" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.NTCI.graffiti.MainActivity" />
    </activity>

but i want it to return to its host fragment not the main activity I have it set to. Any thoughts?

Was it helpful?

Solution

I recently had to do this and decided that a switch statement was the best way to go. Essentially you keep track of what fragment you're in now, and then switch to another one based on whatever criteria you set. For me, it was navigating back from one fragment to another.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
...
case android.R.id.home:
    switch(currentFragment){
    case FRAGMENT1:
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction
        .replace(R.id.fragment_container, fragment2);
        transaction.commit();
        currentFragment = FRAGMENT_2;
        return true;

    default:
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction
        .replace(R.id.fragment_container, fragment1);
        transaction.commit();
        currentFragment = FRAGMENT_1;
        return true;
    }
}

I'm assuming you mean you'd want to end the current activity you're in and display a fragment? In this case there'd probably be a parent FragmentActivity (to host the fragment), so you'd just end the current Activity and start another in the typical way. Once again:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
...
case android.R.id.home:

Intent intent = new Intent(this, NewFragmentActivity.class);
intent.putExtra(...); // so you can pass what activity you're coming from, if needed
startActivity(intent);
this.finish();

Keep in mind this is just a button and you're essentially assigning the onClick() action through case android.R.id.home:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top