2

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?

user3381665
  • 1,131
  • 3
  • 11
  • 16

1 Answers1

3

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:

stewjacks
  • 471
  • 6
  • 12
  • thanks that is very helpful for my fragment to fragment, now any idea on how to do activity back to fragment? – user3381665 Apr 28 '14 at 00:13
  • 1
    thanks for all your help, I get this error. I cant define in my manifest its a fragment but the parent activity is defined am i doing something wrong android.content.ActivityNotFoundException: Unable to find explicit activity class {com.NTCI.graffiti/com.NTCI.graffiti.WhatsHotFragment}; have you declared this activity in your AndroidManifest.xml? – user3381665 Apr 28 '14 at 03:13
  • I'd suggest going through this: http://developer.android.com/guide/components/fragments.html. You need to declare each of your activities in your manifest file, not your fragments. You don't declare fragments in your manifest as they're subsets of your activities. Also you can't use `startActivity` on a fragment because it's not an activity. If you're having trouble declaring activities in your manifest I'd suggest posing another question with that problem with appropriate excepts and myself or someone else can look through it. – stewjacks Apr 28 '14 at 05:35
  • @stewjacks What's the object type of `currentFragment`? – Sndn Feb 09 '15 at 10:57
  • while I don't really remember what I was talking about, I would assume whatever fragment sub-class your heart desires – stewjacks Feb 09 '15 at 21:27