1
   public boolean onKeyDown(int keyCode, KeyEvent event)  {
     if ((keyCode == KeyEvent.KEYCODE_BACK)) {
         moveTaskToBack(true);
         return true;
     }

     return super.onKeyDown(keyCode, event);
   }

嗨,有此代码可以返回上一课,但是当我点击返回按钮时,我进入了我的主启动器屏幕。我想进入我的应用程序的上一个屏幕并保留我在设置菜单中所做的更改。

4

2 回答 2

1

尝试添加这个而不是 moveTaskToBack():

super.onBackPressed();

所以你的代码应该是这样的:

  public boolean onKeyDown(int keyCode, KeyEvent event)  {
     if ((keyCode == KeyEvent.KEYCODE_BACK)) {
         super.onBackPressed();
         return true;
     }

     return super.onKeyDown(keyCode, event);
   }

显然,如果您在应用程序的第一个活动中,您将被带到您的主屏幕

于 2013-01-26T23:54:28.963 回答
0

这是我的主要活动

公共类 MainActivity 扩展 FragmentActivity {

SectionsPagerAdapter mSectionsPagerAdapter;


ViewPager mViewPager;

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


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

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

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_main, menu);  
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.menu_settings4:
            mainactivity();
            return true;
        case R.id.menu_settings1:
            ipallilos();
            return true;
        case R.id.menu_settings2:
            rithmisi();
            return true;
        case R.id.menu_settings3:
            sxetika();
            return true;

        default:
            return super.onOptionsItemSelected(item);
    }

}

private void mainactivity() {

    Toast.makeText(MainActivity.this, "Επιστροφή στην Αρχική Σελίδα", 
            Toast.LENGTH_SHORT).show();
}
private void rithmisi() {
    setContentView(R.layout.rithmisis);
    Toast.makeText(MainActivity.this, "Loading Settings", 
            Toast.LENGTH_SHORT).show();
}

private void ipallilos() {
    setContentView(R.layout.ipallilos);
    Toast.makeText(MainActivity.this, "Loading Staff", 
            Toast.LENGTH_SHORT).show();
}

private void sxetika() {
    //anoigma menou efarmogis
    setContentView(R.layout.sxetikame);
    Toast.makeText(MainActivity.this, "Loading about Software", 
            Toast.LENGTH_SHORT).show();


}

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event)  {
     if ((keyCode == KeyEvent.KEYCODE_BACK)) {
         //moveTaskToBack(true);
         super.onBackPressed();
         return true;
     }

     return super.onKeyDown(keyCode, event);
 }




public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
        return fragment;
    }

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

    /**
     * εγω γράφεις τον τίτλο που θα φενεται
     * στο γραμμη το paper title
     */

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase();
           // case 1:
              //  return getString(R.string.title_section2).toUpperCase();
            case 1:
                return getString(R.string.title_section3).toUpperCase();
        }
        return null;
    }
}



/**
 * ΕΔΩ ΓΡΑΦΕΙΣ ΓΙΑ ΤΟ ΤΙ ΘΑ ΦΕΝΕΤΑΙ ΜΕΣΑ
 * 
 */
public static class DummySectionFragment extends Fragment {
    public DummySectionFragment() {
    }
    public static final String ARG_SECTION_NUMBER = "section_number";



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

        Bundle args = getArguments();
        switch (args.getInt(ARG_SECTION_NUMBER)){

        case 1: 
            return inflater.inflate(R.layout.paraggeleialayout, container, false);

        case 2:
             return inflater.inflate(R.layout.trapezia, container, false);

    }

        return getView();

}
}

}

于 2013-01-27T22:25:07.150 回答