title: Move to a particular tab of bottom navigation from an activity
body: <p>I am working on a project in which I used Bottom Navigation Bar. On Nav Bar, I used 4 menu items(Profile, Home, Reservation, Logout), for them I used fragment.
  Moving to Home, a list of restaurant will be appear. When the user tap on any restaurant from the list, an activity( name ReservationInfo ) will be opened. There are 3 edit text fields and a button in ReservationInfo activity. When the user click on the button it will move to the fragment(Reservation) which is placed on third position of bottom nav bar.</p>
  
  <p>The question is how to move from activity to fragmentand the fragment is set to the 3rd menu item of the bottom navigation bar.</p>
  
  <p>Here is the code:</p>
  
  <p><strong>Resrvation.java</strong></p>
  
  <pre><code>package restaurantlocator.application;
  
  import android.content.DialogInterface;
  import android.support.v7.app.AlertDialog;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.text.TextUtils;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ImageButton;
  import android.widget.Toast;
  
  
  public class Resrvation extends AppCompatActivity {
  
      Button reserve;
      ImageButton imageButton;
      EditText food, chooseTime, date;
      DataBaseHelper db;
  //  ReservationInfo reservationInfo;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_resrvation);
          db = new DataBaseHelper(this);
  
          food = (EditText) findViewById(R.id.etfood);
          chooseTime = (EditText) findViewById(R.id.ettime);
          date = (EditText) findViewById(R.id.edate);
          reserve = (Button) findViewById(R.id.btnreserve);
          imageButton = (ImageButton) findViewById(R.id.imgButton);
          imageButton.setOnClickListener(new View.OnClickListener()
          {
              @Override
              public void onClick(View v)
              {
                  finish();
              }
          });
  
          //reservationInfo = new ReservationInfo();
  
          reserve.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  if (!emptyValidation()) {
                      db.AddData(new Data(food.getText().toString(),
                              chooseTime.getText().toString(),
                              date.getText().toString()));
                      AlertMessage();
                      food.setText(null);
                      chooseTime.setText(null);
                      date.setText(null);
  
                  } else {
                      Toast.makeText(Resrvation.this, "Empty Fields", Toast.LENGTH_SHORT).show();
                  }
              }
  
              private void AlertMessage() {
                  AlertDialog.Builder builder = new AlertDialog.Builder(Resrvation.this);
                  builder.setTitle("Reserved!");
                  builder.setMessage("A notification will be send on your device regarding your reservation.");
                  builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
  
                              //move to fragment ReservationInfo
                      }
                  });
                  builder.show();
              }
          });
      }
  
      private boolean emptyValidation() {
          if (TextUtils.isEmpty(food.getText().toString()) || TextUtils.isEmpty(chooseTime.getText().toString())) {
              return true;
          } else {
              return false;
          }
      }
  }
  </code></pre>
  
  <p><strong>BottomNavigation.java</strong></p>
  
  <pre><code>package restaurantlocator.application;
  
  import android.content.Intent;
  import android.support.annotation.NonNull;
  import android.support.design.widget.BottomNavigationView;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentTransaction;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.MenuItem;
  import android.widget.FrameLayout;
  
  public class BottomNavigation extends AppCompatActivity {
  
      private BottomNavigationView mMianNav;
      private FrameLayout mMainFrame;
      private HomeActivity homeActivity;
      private ReservationInfo reservationInfo;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_bottom_navigation);
          startService(new Intent(this, NotificationService.class));
  
          mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
          mMianNav = (BottomNavigationView) findViewById(R.id.main_nav);
  
          homeActivity = new HomeActivity();
          reservationInfo = new ReservationInfo();
          setFragment(homeActivity);
  
  
          //Listener for handling selection events on bottom navigation items
          mMianNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
              @Override
              public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
  
                  switch (menuItem.getItemId()) {
  
                      case R.id.tohome:
                          setFragment(homeActivity);
                          return true;
  
                      case R.id.toresrvation:
                          setFragment(reservationInfo);
                          return true;
  
                      case R.id.tologout:
                          logout();
                          return true;
  
                      default:
                          return false;
                  }
              }
  
              private void logout() {
                  Intent intent = new Intent(BottomNavigation.this, Registration.class);
                  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                  startActivity(intent);
                  finish();
              }
  
          });
      }
  
      private void setFragment(Fragment fragment) {
          FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
          fragmentTransaction.replace(R.id.main_frame, fragment);
          fragmentTransaction.commit();
          fragmentTransaction.addToBackStack(null);
      }
  
      public void onBackPressed() {
          Intent homeIntent = new Intent(Intent.ACTION_MAIN);
          homeIntent.addCategory(Intent.CATEGORY_HOME);
          homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
          startActivity(homeIntent);
          finish();
  
      }
  
  }
  </code></pre>