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

これが私の主な活動です

public class MainActivity extends 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 に答える