[英]How can I launch a new Activity to a Fragment that is not the initial fragment?
[英]How can I launch specific fragment at launch android app
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.firebase.auth.FirebaseAuth;
public class MainActivity extends AppCompatActivity {
BottomNavigationView bottomNavigationView;
Fragment selectedFragment = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try
{
this.getSupportActionBar().hide();
}
catch (NullPointerException e){}
setContentView(R.layout.activity_main);
bottomNavigationView = (BottomNavigationView)
findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.homee:
selectedFragment = new HomeFragment();
break;
case R.id.nav_search:
selectedFragment = new SearchFragment();
break;
case R.id.nav_notif:
selectedFragment = null;
startActivity(new Intent(MainActivity.this ,SearchFragment.class));
break;
}
if (selectedFragment != null){
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectedFragment).commit();
}
return true;
}
});
}
}
這都是我的主要活動代碼......我應該怎么做,當用戶進入應用程序時,默認情況下會按照此代碼打開主頁片段,當用戶進入應用程序時,他需要按下主頁按鈕才能進入片段...我如何默認情況下,例如 instagram,一旦用戶進入它,它將用戶留在主頁片段我怎么能,我使用 android 材料底部導航如果你問我也可以提供此活動的 xml 文件..謝謝你
設置setOnNavigationItemSelectedListener
后嘗試此代碼
bottomNavigationView.getMenu().findItem(R.id. homee).setChecked(true)
在您的 MainActivity onCreate 方法中嘗試此操作。
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.fragment_container, new ExploreMainFragment());
ft.commit();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.