英文:
Need Each fragment to return to home screen when user presess back(without any button in app)
问题
你好,以下是代码的翻译部分:
SO,我正在创建这个包含不同片段的基本应用程序,并且我希望当用户在他的手机上按下返回按钮时,应用程序返回到主页片段。
主活动
如果您需要更多帮助,请随时提出具体问题。
英文:
SO, I'm creating this basic app that contains different fragments and I want that when the user presses the back button on his phone, The app returns to the home fragment.
Main Activity
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
public void book(View view)
{
Toast.makeText(this, "You tried to book", Toast.LENGTH_SHORT).show();
}
public void submit(View view)
{
EditText editText = (EditText) findViewById(R.id.feedbackText);
if (editText.getText().toString().isEmpty())
{
Toast.makeText(this, "Please type something first", Toast.LENGTH_SHORT).show();
}
else
{
editText.setText("");
Toast.makeText(this, "Thank You for your feedback", Toast.LENGTH_SHORT).show();
closeKeyboard();
}
}
private void closeKeyboard()
{
View view = this.getCurrentFocus();
if (view != null)
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(),0);
}
}
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
Toolbar toolbar, searchToolbar;
NavigationView navigationView;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
Menu search_menu;
MenuItem item_search;
Button submitBtn;
MaterialSearchView searchView;
EditText email;
EditText password;
TextView titleText;
int counter = 0;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search, menu);
MenuItem item = menu.findItem(R.id.action_search);
searchView.setMenuItem(item);
return true;
}
public void guest(View view)
{
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Home());
fragmentTransaction.commit();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.toolbar);
submitBtn = findViewById(R.id.submitBtn);
titleText = findViewById(R.id.toolbarTitle);
searchToolbar = (Toolbar)findViewById(R.id.search_toolbar);
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(Color.parseColor("#FFFFFF"));
searchView=(MaterialSearchView)findViewById(R.id.search_view);
drawerLayout = findViewById(R.id.drawer);
navigationView = findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.Open, R.string.Close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
actionBarDrawerToggle.syncState();
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Home());
fragmentTransaction.commit();
titleText.setText(R.string.home);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
drawerLayout.closeDrawer(GravityCompat.START);
if (item.getItemId() == R.id.home)
{
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Home());
fragmentTransaction.commit();
titleText.setText(R.string.home);
}
if (item.getItemId() == R.id.myOrders)
{
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new MyOrders());
fragmentTransaction.commit();
titleText.setText(R.string.myOrders);
}
if (item.getItemId() == R.id.refer)
{
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Refer());
fragmentTransaction.commit();
titleText.setText(R.string.refer);
}
if (item.getItemId() == R.id.contact)
{
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Contact());
fragmentTransaction.commit();
titleText.setText(R.string.contact);
}
if (item.getItemId() == R.id.feedback)
{
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Feedback());
fragmentTransaction.commit();
titleText.setText(R.string.feedback);
}
if (item.getItemId() == R.id.settings)
{
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Settings());
fragmentTransaction.commit();
titleText.setText(R.string.setting);
}
if (item.getItemId() == R.id.terms)
{
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Terms());
fragmentTransaction.commit();
titleText.setText(R.string.terms);
}
return true;
}
}
Please help me to figure this out.
答案1
得分: 1
在您的Activity
内部使用onBackPress
,请尝试以下方法:
@Override
public void onBackPressed() {
if (!(getSupportFragmentManager().findFragmentById(R.id.container) instanceof Home)) {
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Home());
fragmentTransaction.commit();
titleText.setText(R.string.home);
} else {
super.onBackPressed();
}
}
英文:
using onBackPress
inside your Activity
try this
@Override
public void onBackPressed() {
if (!(getSupportFragmentManager().findFragmentById(R.id.container) instanceof Home)) {
fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new Home());
fragmentTransaction.commit();
titleText.setText(R.string.home);
} else {
super.onBackPressed();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论