Search code examples
androidandroid-activityfragmentandroid-tabsswipeview

Swipe Views with Tabs : Activity or Fragment?


I'm trying to code a swipe views with tabs (3 tabs). I want each 'page' to be an Activity. I'm using the code in this tutorial, but unfortunately each 'page' there can only be a Fragment.

What i tried to do is to transform my Activities into Fragments (but i'm still getting some NullPointerException).

Is it safe to use a Fragment instead of an Activity (in which i'm using : AlarmManager, OnCheckedChangeListener, PackageManager, AlertDialog...)? Is there a better way to solve my issue?

Thank you.

======================================================================

(UPDATE 1)

Here is some code from the converted Fragment :

public class TabFragment extends Fragment implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {

private HashMap<String, String> myMap;
static private TextView usernameView;
private String mUsername;
private String permPassword;
private SharedPreferences pref;
private SharedPreferences prefCheckBox;
private TextView trackTextView;
private TextView logoutTextView;
private TextView settingsTextView;
private CheckBox trackCheckBox;
boolean isTrackingEnabled;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view =  inflater.inflate(R.layout.activity_home_profile, container, false);

    final SessionManager session = new SessionManager(getActivity().getApplicationContext());
    myMap = session.getUserDetails();
    usernameView = (TextView) getActivity().findViewById(R.id.usernameText);
    mUsername = myMap.get("username");
    usernameView.setText("");
    usernameView.setText(mUsername);

    trackTextView = (TextView) view.findViewById(R.id.tracking_textView);
    trackCheckBox = (CheckBox) view.findViewById(R.id.track_checkBox);
    logoutTextView = (TextView) view.findViewById(R.id.log_out);
    settingsTextView = (TextView) view.findViewById(R.id.settings);

    prefCheckBox = getActivity().getApplicationContext().getSharedPreferences("Tracking Checkbox", 0); // 0 - for private mode
    isTrackingEnabled = prefCheckBox.getBoolean("CheckBoxState", false);
    // display checkbox previous state
    trackCheckBox.setChecked(isTrackingEnabled);

    logoutTextView.setOnClickListener(this);
    trackTextView.setOnClickListener(this);
    settingsTextView.setOnClickListener(this);

    trackCheckBox.setOnCheckedChangeListener(this);

    return view;
}

I am getting a NullPointerException for usernameView.setText("")

02-10 15:03:35.203  27866-27866/com.projectname E/AndroidRuntime﹕ FATAL EXCEPTION: main

java.lang.NullPointerException

Solution

  • You can't use multiple activity in a single page.

    The only way is fragments,Fragments are build to support this feature.

    A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).

    Converting Activity into fragment is very simple

    1. In case of activity you are extending Activity change it to Fragment
    2. Change your onCreate function to onCreateView

    Eg :

    Activity-

    public class MainActivity extends Activity {
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
            }   
        }
    

    Converted Fragment-

    public class MainActivity extends Fragment {
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
            View  view=inflater.inflate(R.layout.activity_main,null);
    
            return view;
        }
    }
    

    Most important

    Inside activity usually we are used to find view from xml using this method

    TextView textView= (TextView) findViewById(R.id.helletxtview);
    

    Inside fragments this will not work because findViewById not available in Fragment class,But use can use with the help of root View like this

    View view=inflater.inflate(R.layout.activity_main,null);
    TextView textView= (TextView) view.findViewById(R.id.helletxtview);