我開發一個Android應用程序,我有一個名爲「更多」片段,在這個片段我有這樣的事情,「關於我們」,「條款與條件」 ,這是正常的活動。我想要做的是,當我點擊後面的按鈕,例如,關於我們的活動,我想返回到「更多」片段。我怎樣才能做到這一點?這是我在aboutUs活動中所具有的:從活動讓後退按鈕返回到片段的Android
「onBackPressed方法應該回到片段,一旦我點擊後退按鈕,但它不起作用。應用程序崩潰,並在日誌貓說「你有沒有申報MoreFragment活動在清單?」,但我想聲明,它不工作
public class AboutUs extends BaseActivity {
private ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about_us);
image = (ImageView) findViewById(R.id.imageView2);
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent launchactivity = new Intent(AboutUs.this, StartScreenActivity.class);
startActivity(launchactivity);
}
});
}
@Override
public void onBackPressed()
{
Intent intent = new Intent(this,MoreFragment.class);
startActivity(intent);
}
}
這是調用活動中的片段:
public class MoreFragment extends Fragment implements View.OnClickListener {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_more, null, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView lbl1 = (TextView) view.findViewById(R.id.lbl1);
TextView lbl2 = (TextView) view.findViewById(R.id.lbl2);
TextView lbl3 = (TextView) view.findViewById(R.id.lbl3);
TextView lbl4 = (TextView) view.findViewById(R.id.lbl4);
TextView lbl5 = (TextView) view.findViewById(R.id.lbl5);
TextView lbl6 = (TextView) view.findViewById(R.id.lbl6);
TextView lbl7 = (TextView) view.findViewById(R.id.lbl7);
TextView lbl8 = (TextView) view.findViewById(R.id.lbl8);
TextView lbl9 = (TextView) view.findViewById(R.id.lbl9);
ImageView imv1 = (ImageView) view.findViewById(R.id.imv1);
ImageView imv2 = (ImageView) view.findViewById(R.id.imv2);
ImageView imv3 = (ImageView) view.findViewById(R.id.imv3);
ImageView imv4 = (ImageView) view.findViewById(R.id.imv4);
ImageView imv5 = (ImageView) view.findViewById(R.id.imv5);
ImageView imv6 = (ImageView) view.findViewById(R.id.imv6);
ImageView imv7 = (ImageView) view.findViewById(R.id.imv7);
ImageView imv8 = (ImageView) view.findViewById(R.id.imv8);
ImageView imv9 = (ImageView) view.findViewById(R.id.imv9);
ImageView imv10 = (ImageView) view.findViewById(R.id.imv10);
TextView btnSignOut = (TextView) view.findViewById(R.id.lbl10);
lbl1.setOnClickListener(this);
lbl2.setOnClickListener(this);
lbl3.setOnClickListener(this);
lbl4.setOnClickListener(this);
lbl5.setOnClickListener(this);
lbl6.setOnClickListener(this);
lbl7.setOnClickListener(this);
lbl8.setOnClickListener(this);
lbl9.setOnClickListener(this);
imv1.setOnClickListener(this);
imv2.setOnClickListener(this);
imv3.setOnClickListener(this);
imv4.setOnClickListener(this);
imv5.setOnClickListener(this);
imv6.setOnClickListener(this);
imv7.setOnClickListener(this);
imv8.setOnClickListener(this);
imv9.setOnClickListener(this);
btnSignOut.setOnClickListener(this);
View view9 = view.findViewById(R.id.view9);
View view8 = view.findViewById(R.id.view8);
if (!PrefernceHelper.getBoolean(getActivity(), Commons.Constants.USERLOGGEDIN)) {
imv8.setVisibility(View.INVISIBLE);
lbl8.setVisibility(View.INVISIBLE);
imv10.setVisibility(View.INVISIBLE);
btnSignOut.setVisibility(View.INVISIBLE);
view9.setVisibility(View.INVISIBLE);
view8.setVisibility(View.INVISIBLE);
}
}
private void callAllMoreScreen(String type, Class<?> className) {
Intent intent = new Intent();
intent.setClass(getActivity(), className);
intent.putExtra(Commons.Constants.LOAD_TYPE_URL, type);
getActivity().startActivity(intent);
}
private void callAllMoreScreen1(Class<?> className) {
Intent intent = new Intent();
intent.setClass(getActivity(), className);
getActivity().startActivity(intent);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.lbl1:
callAllMoreScreen1(AboutUs.class);
break;
case R.id.lbl2:
callAllMoreScreen("contact-us", AllMoreScreenActivity.class);
break;
case R.id.lbl3:
callAllMoreScreen1(TermsConditions.class);
break;
case R.id.lbl4:
callAllMoreScreen1(PrivacyPolicy.class);
break;
case R.id.lbl5:
callAllMoreScreen("faqs", AllMoreScreenActivity.class);
break;
case R.id.lbl6:
callAllMoreScreen("how-it-work", AllMoreScreenActivity.class);
break;
case R.id.lbl7:
callAllMoreScreen("about-foodlebee", AllMoreScreenActivity.class);
break;
case R.id.lbl8:
callAllMoreScreen(" ", PredefinedAddressActivity.class);
break;
case R.id.lbl9:
callAllMoreScreen(" ", TrackyourorderActivity.class);
break;
case R.id.imv1:
callAllMoreScreen1(AboutUs.class);
break;
case R.id.imv2:
callAllMoreScreen("contact-us", AllMoreScreenActivity.class);
break;
case R.id.imv3:
callAllMoreScreen1(TermsConditions.class);
break;
case R.id.imv4:
callAllMoreScreen1(PrivacyPolicy.class);
break;
case R.id.imv5:
callAllMoreScreen("faqs", AllMoreScreenActivity.class);
break;
case R.id.imv6:
callAllMoreScreen("how-it-work", AllMoreScreenActivity.class);
break;
case R.id.imv7:
callAllMoreScreen("about-foodlebee", AllMoreScreenActivity.class);
break;
case R.id.imv8:
callAllMoreScreen(" ", PredefinedAddressActivity.class);
break;
case R.id.imv9:
callAllMoreScreen(" ", TrackyourorderActivity.class);
break;
case R.id.lbl10:
PrefernceHelper.clearAll(getActivity());
callAllMoreScreen("", SignInActivity.class);
getActivity().finish();
break;
}
}
}
這是調用片段的Activity:
public class TabLayoutScreenActivity extends BaseActivity {
private TabLayout tabLayout;
private Bundle bundle;
//Bundle bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_screen);
tabLayout = (TabLayout) findViewById(R.id.tab_layout);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar); // actionBarSetUp(true);
// getSupportActionBar().setIcon(R.drawable.small_bee);
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// getSupportActionBar().setIcon(R.drawable.ic_launcher);
viewPager.setAdapter(new SectionPagerAdapter(getSupportFragmentManager()));
tabLayout.setupWithViewPager(viewPager);
ImageView foodlebee = (ImageView) findViewById(R.id.imageView2);
foodlebee.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent launchactivity = new Intent(TabLayoutScreenActivity.this,StartScreenActivity.class);
startActivity(launchactivity);
}
});
// bundle = getIntent().getExtras();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_login, menu);
if (PrefernceHelper.getBoolean(this, Commons.Constants.USERLOGGEDIN)) {
MenuItem item = menu.findItem(R.id.user_login);
item.setVisible(false);
invalidateOptionsMenu();
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.user_login) {
intentCall(SignInActivity.class, null, 0);
return true;
} else if (item.getItemId() == R.id.home) {
intentCall(StartScreenActivity.class, null, 0);
finish();
return true;
}
return false;
}
@Override
protected void onStart() {
super.onStart();
bundle = getIntent().getExtras();
int tabPostion = 0;
if (bundle != null)
tabPostion = bundle.getInt(Commons.Constants.TAB_POSITION, 0);
TabLayout.Tab tab = tabLayout.getTabAt(tabPostion);
tab.select();
}
public class SectionPagerAdapter extends FragmentPagerAdapter {
public SectionPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ChefFragment();
case 1:
return new OrderFragment();
case 2:
return new BasketFragment();
case 3:
return new MoreFragment();
default:
return new ChefFragment();
}
}
@Override
public int getCount() {
return 4;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Chefs";
case 1:
return "Orders";
case 2:
return "Cart";
case 3:
return "More";
default:
return "Chef";
}
}
}
}
這是兩個層面內?當您點擊about片段中的後退按鈕時,現在會發生什麼? –
在這種情況下,請勿重寫'onBackPress'方法。它應該會自動工作。 – jaibatrik