Daveak Daveak - 26 days ago 6
Android Question

SplashScreen Activity onClick to open MainActivity and replace fragment in that Activity's fragment container

Hi really struggling with this one, hence my first post after a day of searching for a solution with no luck.

I have a splash screen which has an ImageView with an onClickListener which when clicked opens up my MainActivity just fine:

SplashActivity.java



bookMain.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View view) {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
}


However what I'm trying to achieve is, when the button is clicked, it opens my MainActivity, but replace's the fragment_container in the MainActivity I have with another fragment (this is how my bottombar Navigation works).

This is the code in the MainActivity that handles the replacing of fragments when navigation buttons are clicked and essentially I want the Splash Screen's click to open up the "BookFragment" rather than go straight to the "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();
}


}


});


Corrected working SplashActivity.java:



In the onCreate method:

//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;
}
}


Corrected working MainActivity.java



In the onCreate method (enter the position of the tab of the bottomBar relevant to you):

Intent intent = getIntent();
String source = intent.getStringExtra("source");
if (source != null && source.equals("onClick")) {
bottomBar.selectTabAtPosition(4);
}


Good luck!

Answer

because you use this:

getSupportFragmentManager().beginTransaction()
            .add(R.id.fragment_container, firstFragment).commit();

it's will show the firstFragment default

you can try this:

In Spalsh Activity

Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);

add some params for intent

Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.putExtra("source","onClick");   //add like this line
startActivity(intent);

In 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);
    }
}

when Main Activity incept the params source,and the source value is your set, it's will run the code in if