Android Open Source - FragmentTutorial Main Activity






From Project

Back to project page FragmentTutorial.

License

The source code is released under:

Apache License

If you think the Android project FragmentTutorial listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.yanlu.android.fragment;
//from ww w .  j  av  a  2  s .  c o  m
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.yanlu.android.fragment.frg.LeftFragment;
import com.yanlu.android.fragment.frg.RightFragment;
import com.yanlu.android.fragment.model.DemoParcel;


public class MainActivity extends ActionBarActivity implements LeftFragment.OnFragmentInteractionListener {
    private static final String TAG = "MainActivity";

    private final int TAB_NUM = 2;
     private int preTabIdx = 0;
     private int curTabIdx = -1;
     private Fragment[] tabFragments = new Fragment[TAB_NUM];
     private TextView[] rl_tabs = new TextView[TAB_NUM];
    private boolean clicking = false;
    private Handler handler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate(savedInstanceState)");
        setContentView(R.layout.activity_main);

        rl_tabs[0] = (TextView) findViewById(R.id.tv_tab_left);
        rl_tabs[1] = (TextView) findViewById(R.id.tv_tab_right);

        for (int idx = 0; idx < TAB_NUM; idx++) {
            final int i = idx;
            rl_tabs[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    clickTab(i);
                }
            });
        }


        if (savedInstanceState == null) {
            clickTab(0);
        } else {
            setSelectedTab(curTabIdx = savedInstanceState.getInt("curTabIdx"));
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, 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_settings) {
//            Toast.makeText(MainActivity.this, "Pressed Settings", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this, SettingsActivity.class));
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    private void clickTab(int i) {
       if (clicking){
         return;
        }

       if (i < 0 || i > TAB_NUM - 1) {
         i = 0;
       }
       if (curTabIdx != i) {

         curTabIdx = i;
         replaceFragment();

        }
        setSelectedTab(curTabIdx);
    }

    private void setSelectedTab(int curTabIdx) {
       for (int idx = 0; idx < TAB_NUM; idx++) {
         if (idx == curTabIdx) {
           rl_tabs[idx].setSelected(true);
         } else {
           rl_tabs[idx].setSelected(false);
         }
       }
     }

    private synchronized void replaceFragment() {
       clicking = true;
       if (tabFragments[preTabIdx] == null || tabFragments[curTabIdx] == null) {
         initFragment();
       }

       FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.container, tabFragments[curTabIdx]);
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
//        if(curTabIdx == 0) {
            fragmentTransaction.addToBackStack(null);
//        }
        fragmentTransaction.commit();
       // Workaround for Issue 42601:
       // https://code.google.com/p/android/issues/detail?id=42601
//       fragmentTransaction.detach(tabFragments[preTabIdx]).replace(R.id.tab_content, tabFragments[curTabIdx])
//           .attach(tabFragments[curTabIdx]).commitAllowingStateLoss();
       preTabIdx = curTabIdx;


       handler.postDelayed(new Runnable() {
         @Override
         public void run() {
           clicking = false;
         }
       }, 500);
     }

    private void initFragment() {
       tabFragments[0] = new LeftFragment();
        DemoParcel parcel = new DemoParcel();
        parcel.setId(16916);
        parcel.setName("captain_admin");
        tabFragments[1] = RightFragment.newInstance("fragment", parcel);
       preTabIdx = 0;
     }

    public void showDialog(View v) {
        new AlertDialog.Builder(this)
            .setTitle(R.string.dialog_title)
            .setMessage(R.string.dialog_message)
            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                    Toast.makeText(MainActivity.this, "Pressed OK", Toast.LENGTH_SHORT).show();
                }
            })
            .setNegativeButton(R.string.dismiss, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            }).create().show();
    }

    @Override
    public void onFragmentInteraction() {
        Toast.makeText(MainActivity.this, "onFragmentInteraction()", Toast.LENGTH_SHORT).show();
    }

    public String getStringFromActivity(){
        return TAG;
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart( )");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart( )");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume( )");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop( )");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.d(TAG, "onSaveInstanceState(Bundle outState)");
        outState.putInt("curTabIdx", curTabIdx);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause( )");
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy( )");
    }

}




Java Source Code List

com.yanlu.android.fragment.App.java
com.yanlu.android.fragment.MainActivity.java
com.yanlu.android.fragment.SettingsActivity.java
com.yanlu.android.fragment.frg.FragmentByXml.java
com.yanlu.android.fragment.frg.LeftFragment.java
com.yanlu.android.fragment.frg.RightFragment.java
com.yanlu.android.fragment.model.DemoDataMo.java
com.yanlu.android.fragment.model.DemoParcel.java
com.yanlu.android.fragment.net.GsonRequest.java
com.yanlu.android.fragment.net.LruBitmapCache.java
com.yanlu.android.fragment.net.RequestManager.java