public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
你好这个代码可以回到上一课,但是当我按回按钮时,我会进入我的家庭启动器屏幕。 我想要访问我之前的应用程序屏幕并保留我在设置菜单中所做的更改。
答案 0 :(得分: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);
}
显然,如果您正在使用应用程序中的第一个活动,那么您将被带到主屏幕
答案 1 :(得分: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();
}
}
}