首页 > 代码库 > ActionBar Fragment的一个sample activity

ActionBar Fragment的一个sample activity

public class MainActivity extends ActionBarActivity {
    private static ActivityManager am;
    private static List<RunningAppProcessInfo>  appList;
    private static List<RunningTaskInfo> taskList; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
        
        
        am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        
        appList = am.getRunningAppProcesses();
        taskList = am.getRunningTasks(100);
        
        for(RunningAppProcessInfo info: appList){
            Log.d("linsen",""+info.processName);
        }
        for(RunningTaskInfo info: taskList){
            Log.i("linsen", ""+info.topActivity);
        }
        
        
    }



    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        private Button btn2;
        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            btn2  = (Button) rootView.findViewById(R.id.button1);
            btn2.setOnClickListener(new OnClickListener() {
                
                @SuppressLint("NewApi")
                @Override
                public void onClick(View v) {
                    
                    am.moveTaskToFront(taskList.get(4).id,0);
                    Log.w("linsen","btn2 onclicked");
                    moveTask();
                }

                
            });
            return rootView;
        }
    }
    private static void moveTask() {
        // TODO Auto-generated method stub
        
    }
    
    @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) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

 About Fragment, there are 3 import methods:     Fragment.onCreateView;     Activity.getSupportFragmentManager;  FragmentManager.beginTransaction; 

 Fragment.onCreateView;    

Activity.getSupportFragmentManager;  

FragmentManager.beginTransaction;