【问题标题】:Move to a particular tab of bottom navigation from an activity从活动移动到底部导航的特定选项卡
【发布时间】:2018-10-17 15:25:07
【问题描述】:

我正在做一个使用底部导航栏的项目。在 Nav Bar 上,我使用了 4 个菜单项(Profile、Home、Reservation、Logout),为此我使用了片段。 移动到首页,将出现餐厅列表。当用户点击列表中的任何餐厅时,将打开一个活动(名称 ReservationInfo )。 ReservationInfo 活动中有 3 个编辑文本字段和一个按钮。当用户点击按钮时,它将移动到位于底部导航栏第三位置的片段(预订)。

问题是如何从activity移动到fragment,fragment设置到底部导航栏的第3个菜单项。

代码如下:

Resrvation.java

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

BottomNavigation.java

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();

    }

}

【问题讨论】:

  • 您能否详细说明您的问题..
  • 如何从一个活动(名为 Resrvation)移动到一个片段?该片段名称是 ResrvationInfo。当片段设置为底部导航栏的第三个菜单项时。
  • 我无法详细说明问题。 :(
  • 必须从活动移动到片段,并且该片段设置为底部导航栏的第三个菜单项。
  • 我得到了那个部分,片段是在同一个活动中还是在不同的活动中,我很困惑。

标签: android android-activity fragment


【解决方案1】:

解决方法:

按照以下步骤操作:

Step1:添加如下所示的行reserveclick listener

reserve.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        ....
        ....
        Intent intent = new Intent(Resrvation.this, BottomNavigation.class);
        intent.putExtra("FromReservation", "1");
        startActivity(intent);
        ....
    }
}

接下来,Step2:在onCreate() of BottomNavigation class 中编写以下代码:

Intent i = getIntent();
String data = i.getStringExtra("FromReservation");

if (data != null && data.contentEquals("1")) {

    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.main_frame, new ReservationInfo());
    fragmentTransaction.commitNow();

    mMianNav.setSelectedItemId(R.id.toresrvation);

}

试试看,如果有问题,请在下方评论。

【讨论】:

  • @AmnaMughal 好的
  • 只有将mMianNav.setSelectedItemId(R.id.toresrvation); 放在FragmentTransaction..... 上方才有效。
  • 是的,但是你在 onCreate() 中声明它,它会去 :) @AmnaMughal
  • 也使用setFragment(reservationInfo);而不是FragmentTransaction........。
  • 哦是的,对不起,我拿的是内容而不是函数。
【解决方案2】:

当用户从您的家庭片段中选择一家餐厅时,您只需使用startActivityForResults 而不是startActivity。

例如:

startActivityForResult(yourIntent, 1000);

并在onClick函数中的reservationInfo Activity上使用

Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();

然后在您的第一个活动中覆盖 onActivityResult() 方法并在请求代码匹配时将所选片段设置为保留

例如:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

if (requestCode == 1000) {
    mMianNav.setSelectedItemId(R.id.toresrvation);
}

}

【讨论】:

  • restaurationinfo 中没有使用onClick 方法,只是一个列表。
猜你喜欢
  • 1970-01-01
  • 2016-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-10
  • 2020-12-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多