Android:按主页按钮返回第一个片段

时间:2015-07-30 18:47:23

标签: android android-fragments

我正在开发一个简单的Android应用程序,它具有MainActivity和FragmentStatePagerAdapter以及一些并排的片段。我希望能够通过按主页按钮转到第一个片段。

我知道我的代码非常糟糕。我仍然希望只有几行能解决我的问题。

这是我的MainActivity

package com.freestylers.druskischool;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends FragmentActivity {

ViewPager pager;
FragmentStatePagerAdapter adapter; 


* Each page of our pager will display one fragment from this array
* Swiping, to the right will take you to the next page
*/
 String[] fragments={
    "start",
    "mes",
    "whatever",
    "next",
    "name",
    "of",
    "a",
    "fragment",
    "more fragments",
    "and",
    "more",
    "fragments"

};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    pager=(ViewPager)findViewById(R.id.my_pager);

    adapter=new FragmentStatePagerAdapter(
        //maybe should use normal getFragmentManager()
            getSupportFragmentManager()
        ){

        @Override
        public int getCount() {
            // This makes sure getItem doesn't use a position
           // that is out of bounds of our array of fragments
           return fragments.length;
        }

        @Override
        public Fragment getItem(int position) {
            // Here is where all the magic of the adapter happens
            // As you can see, this is really simple.
            //(fragments[position]);
                if(position==0){
                return StartFragment.newInstance();
                }
                else if(position==1){
                return MesFragment.newInstance();
                }
                else if(position==2){
                returnNextFragment.newInstance();
                }
                //this goes on until position==11
                else{return StartFragment.newInstance();}

        }

    };

    //Let the pager know which adapter it is supposed to use
    pager.setAdapter(adapter);  
}  



@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);
}
}

这是我的一个片段,MesFragment。在第19行(用双斜线注释)我只是不知道用哪个命令来获取我的第一个片段:

package com.freestylers.druskischool;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;


public class MesFragment extends Fragment {


@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()){
        case android.R.id.home:
        //(getActivity()).getSupportFragmentManager().??? I don`t know how               

to go back to first fragment
            return true;
        default:
            return super.onOptionsItemSelected(item);
 }
}
@Override
public View onCreateView(LayoutInflater inflater, 
     ViewGroup container, Bundle savedInstanceState) {
    (getActivity()).getActionBar().setDisplayHomeAsUpEnabled(true);

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


    return view;
}

// This is the method the pager adapter will use
// to create a new fragment
public static Fragment newInstance(){
    MesFragment f=new MesFragment();

    return f;
 }

}

由于

2 个答案:

答案 0 :(得分:1)

您可以使用活动中的ViewPager设置当前选定的项目:

public void switchToMesFragment() {
        pager.setCurrentItem(1);
    }

要从片段内切换,您需要获得对活动的引用并调用切换函数,如下所示:

((MainActivity)getActivity()).switchToMesFragment();

答案 1 :(得分:0)

您无法完全控制主页按键,因为它是广播给设备上所有活动应用的密钥。如果要处理应用程序的“主页”按钮,可以覆盖活动。onPause()方法。文档@ Pausing an Activity。但是你仍然无法阻止用户看到主屏幕,这对其他应用程序来说也许不公平。先试试这个......