Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Self-Destructing Message Android App Using Fragments for Tabs Modifying Tabs from the Template

Nicholas Kotsiantos
Nicholas Kotsiantos
8,827 Points

Everything is outdated? Completely lost. What is the current solution?

I've been trying stuff for hours and nothing seems to work.

alex gwartney
alex gwartney
8,849 Points

Could you elaborate on what it is your lost on and what it is your specifically needing help on?

Agreed. The lesson seems to be pretty badly broken.

Luke Liem
Luke Liem
6,367 Points

See my answer below. Try to implement all the code changes in the section before you compile and debug.

3 Answers

Luke Liem
Luke Liem
6,367 Points

I can totally understand the frustration people have with following along the video tutorial in the section. I can only provide the following advice:

(1) Try to implement the codes for the ENTIRE section (the first 4 videos), then compile the code and debug. (2) The codes provided by jenyufu is a good reference point: https://teamtreehouse.com/community/read-this-your-going-to-run-into-some-problems-later-in-this-class (3) The DummySectionFragment code section, which is to be removed, is now the PlaceholderFragment in Android Studio. https://teamtreehouse.com/community/for-ppl-who-cant-find-dummysectionfragment-in-android-studio-ithe-name-has-been-changed-to-placeholderfragment

The following are my codes, which compile and run without any problem. Note that I call my app "Rabbit" instead of "Ribbit"

My Code for MainActivity.java

package com.scipio.luke.rabbit;

import android.app.ActionBar; import android.app.FragmentTransaction; import android.content.Intent; import android.os.Bundle; import android.support.v13.app.FragmentPagerAdapter; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import com.parse.ParseUser;

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

/**
 * The {@link android.support.v4.view.PagerAdapter} that will provide
 * fragments for each of the sections. We use a
 * {@link FragmentPagerAdapter} derivative, which will keep every
 * loaded fragment in memory. If this becomes too memory intensive, it
 * may be best to switch to a
 * {@link android.support.v13.app.FragmentStatePagerAdapter}.
 */
SectionsPagerAdapter mSectionsPagerAdapter;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;
public static final String TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Whenever you use any signup or login methods, the user is cached on disk. By using the
    // cached currentUser object, the user does not have to login everytime he opens the app
    ParseUser currentUser = ParseUser.getCurrentUser();
    if (currentUser == null) {
        navigateToLogin();
    }
    else {
        Log.i(TAG,currentUser.getUsername());
    }


    // Set up the action bar.
    final ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }
}

private void navigateToLogin() {
    // The user is directed to Login Page
    Intent intent = new Intent(this, LoginActivity.class);

    // This removes MainActivity and set the LoginActivity at the bottom of the Stack
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

    startActivity(intent);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_logout) {
        ParseUser.logOut();
        navigateToLogin();
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    // When the given tab is selected, switch to the corresponding page in
    // the ViewPager.
    mViewPager.setCurrentItem(tab.getPosition());
}

@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

@Override
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
}

}

My Code for SectionsPagerAdapter.java

package com.scipio.luke.rabbit;

import android.content.Context; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter;

import java.util.Locale;

/**

  • Created by Luke on 9/16/2015. */

/**

  • A {@link android.support.v13.app.FragmentPagerAdapter} that returns a fragment corresponding to
  • one of the sections/tabs/pages. */ public class SectionsPagerAdapter extends FragmentPagerAdapter {

    private Context mContext;

    public SectionsPagerAdapter(Context context, FragmentManager fm) { super(fm); mContext = context; }

    @Override public Fragment getItem(int position) { // getItem is called to instantiate the fragment for the given page. switch (position) { case 0: return new InboxFragment(); case 1: return new FriendsFragment(); } return null; }

    @Override public int getCount() { // Show 2 total pages. return 2; }

    @Override public CharSequence getPageTitle(int position) { Locale l = Locale.getDefault(); switch (position) { case 0: return mContext.getString(R.string.title_section1).toUpperCase(l); case 1: return mContext.getString(R.string.title_section2).toUpperCase(l); } return null; } }

My Code for FriendsFragment.java

package com.scipio.luke.rabbit;

import android.os.Bundle; import android.support.v4.app.ListFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;

/**

  • Created by Luke on 9/16/2015. */ public class FriendsFragment extends ListFragment {

    //Called when fragment is drawn for the first time @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_friends, container, false);

    return rootView;
    

    }

}

My Code for InboxFragment.java

package com.scipio.luke.rabbit;

import android.os.Bundle; import android.support.v4.app.ListFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup;

/**

  • Created by Luke on 9/16/2015. */ public class InboxFragment extends ListFragment {

    //Called when fragment is drawn for the first time @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_inbox, container, false); return rootView; }

}

Matthew Barrus
Matthew Barrus
16,731 Points

I followed this tutorial and got it working using a TabsLayout. It didn't take too long to get working once I found this tutorial.

https://guides.codepath.com/android/Google-Play-Style-Tabs-using-TabLayout