我真的很纠结这个问题,所以我花了一天时间寻找解决方案,但运气不好。
我有一个闪屏,其中有一个ImageView与onClickListener,当点击打开我的MainActivity只是很好:
SplashActivity.java
bookMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
}
然而,我想要实现的是,当按钮被点击,它打开我的MainActivity,但取代的是在MainActivity的fragment_container我有另一个片段(这就是我的底部栏导航的工作方式)。
这是MainActivity中的代码,当导航按钮被点击时,处理碎片的替换,本质上我希望启动屏幕的点击打开"BookFragment",而不是直接进入"RoomsFragment":
MainActivity.java
public class MainActivity extends AppCompatActivity {
//STANDARD ON CREATE OPEN TO LOAD INITIAL LAYOUT
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//THIS CREATES A BLANK CONTAINER FOR EACH FRAGMENT YOU CREATE TO GO IN
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
RoomsFragment firstFragment = new RoomsFragment();
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
//THIS CREATES THE NAV BAR AND TELLS WHAT FRAGMENT TO PUT IN THE CONTAINER WHEN A TAB IS SELECTED
BottomBar bottomBar = (BottomBar) findViewById(R.id.bottomBar);
bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelected(@IdRes int tabId) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//TAB1 - THE DEFAULT TAB
if (tabId == R.id.tab_rooms) {
RoomsFragment newFragment = new RoomsFragment();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
//TAB2
} else if (tabId == R.id.tab_shisha) {
// THIS BIT BELOW IS WHAT CHANGES THE FRAGMENT IN THE CONTAINER
ShishaFragment newFragment = new ShishaFragment();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
//TAB3
} else if (tabId == R.id.tab_cocktails) {
// THIS BIT BELOW IS WHAT CHANGES THE FRAGMENT IN THE CONTAINER
CocktailsFragment newFragment = new CocktailsFragment();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
//TAB4
} else if (tabId == R.id.tab_food) {
// THIS BIT BELOW IS WHAT CHANGES THE FRAGMENT IN THE CONTAINER
FoodFragment newFragment = new FoodFragment();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
//TAB5
} else if (tabId == R.id.tab_book) {
// THIS BIT BELOW IS WHAT CHANGES THE FRAGMENT IN THE CONTAINER
BookFragment newFragment = new BookFragment();
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
});
纠正工作SplashActivity.java:
在onCreate方法中://ONCLICK LISTENER FOR THE ENTER BUTTON AND BOOK BUTTON ON SPLASH PAGE
Button enterButton = (Button) findViewById(R.id.enter_button);
enterButton.setOnClickListener(SplashActivity.this);
Button bookButton = (Button) findViewById(R.id.book_button);
bookButton.setOnClickListener(SplashActivity.this);
}
public void onClick(View v) {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
switch (v.getId()) {
case R.id.enter_button:
startActivity(intent);
break;
case R.id.book_button:
intent.putExtra("source","onClick");
startActivity(intent);
break;
default:
break;
}
}
修正MainActivity.java
在onCreate方法中(输入与您相关的底部栏选项卡的位置):
Intent intent = getIntent();
String source = intent.getStringExtra("source");
if (source != null && source.equals("onClick")) {
bottomBar.selectTabAtPosition(4);
}
祝你好运!
因为你使用了这个:
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
它会显示firstFragment
的默认值
你可以试试:
Spalsh Activity
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
为intent添加一些参数
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.putExtra("source","onClick"); //add like this line
startActivity(intent);
Main Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
```your code
Intent intent = getIntent();
String source = intent.getStringExtra("source");
if (source.equals("onClick")) {
bottomBar.onTabSelected(R.id.tab_book);
}
}
当Main Activity接收参数source
,并且source
值是您的设置时,它将运行if