简体   繁体   English

如何从活动返回导航底部的特定片段?

[英]How to return from activity to a specific fragment in navigation bottom?

description 描述

I have 3 fragment in navigation bottom , I want to make activity(Sign_With_Google) to enable user to register using Google Sign in , but this activity will appear when i click to chat icon(chat fragment) in navigation bottom if user registered , chat fragment appear automatically , if user not registered , the activity will appear to enable user to register 我在导航底部有3个片段,我想进行活动(Sign_With_Google)以使用户能够使用Google登录进行注册,但是如果用户注册了聊天片段,则当我单击导航底部的聊天图标(聊天片段)时,此活动就会出现自动出现,如果用户未注册,将显示活动以使用户能够注册

MainActivity 主要活动

  import android.app.AlertDialog;
    import android.app.ProgressDialog;
    import android.content.ActivityNotFoundException;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.graphics.Color;
    import android.graphics.Typeface;
    import android.net.Uri;
    import android.os.Build;
    import android.os.Bundle;
    import android.os.Handler;
    import android.support.annotation.NonNull;
    import android.support.annotation.RequiresApi;
    import android.support.design.widget.BottomNavigationView;
    import android.support.design.widget.NavigationView;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTransaction;
    import android.support.v4.view.GravityCompat;
    import android.support.v4.view.MenuItemCompat;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v4.widget.SwipeRefreshLayout;
    import android.support.v7.app.ActionBarDrawerToggle;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.support.v7.widget.Toolbar;
    import android.text.Spannable;
    import android.text.SpannableString;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.SubMenu;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.FrameLayout;
    import android.widget.ProgressBar;
    import android.widget.SearchView;
    import android.widget.Switch;
    import android.widget.TextView;
    import android.widget.Toast;

    import com.firebase.ui.database.FirebaseRecyclerAdapter;
    import com.firebase.ui.database.FirebaseRecyclerOptions;
    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;
    import com.google.firebase.database.Query;
    import com.roger.catloadinglibrary.CatLoadingView;


    public class MainActivity extends AppCompatActivity
            implements NavigationView.OnNavigationItemSelectedListener {

        private BottomNavigationView navigation;
        private Toolbar toolbar;
        private NavigationView navigationView;
        private DrawerLayout drawer;
        private long time;
        TextView txt_title;
        TextView txt_desc;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            navigationView = findViewById(R.id.nav_view);
            FrameLayout frameLayout = findViewById(R.id.frame_container);
            txt_title = findViewById(R.id.txt_title);
            txt_desc = findViewById(R.id.txt_desc);
            TextView textView = (TextView) toolbar.findViewById(R.id.text_toolbar);
            textView.setText("مواضيع");
            drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            navigation = (BottomNavigationView) findViewById(R.id.bottom_navigation);
            navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
            setFragemnt(new Home_Fragment());

            toolbar_text_custom();
            toggle_menu_difinetion();
            navigarion_drawer_font();
            bottom_navigation_font();
            check_net();


        }

        private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
                = new BottomNavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment fragment;
                Typeface typeface;
                switch (item.getItemId()) {
                    case R.id.home:
                        typeface = Typeface.createFromAsset(getAssets(), "Cairo.ttf");
                        TextView textView = (TextView) toolbar.findViewById(R.id.text_toolbar);
                        textView.setText("مواضيع");
                        textView.setTypeface(typeface);
                        fragment = new Home_Fragment();
                        setFragemnt(fragment);
                        return true;
                    case R.id.section:
                        typeface = Typeface.createFromAsset(getAssets(), "Cairo.ttf");
                        TextView text_sec = (TextView) toolbar.findViewById(R.id.text_toolbar);
                        text_sec.setText("أقسام");
                        text_sec.setTypeface(typeface);
                        fragment = new Section_Fragment();
                        setFragemnt(fragment);
                        return true;
                    case R.id.chat:
    /*
                        typeface = Typeface.createFromAsset(getAssets(), "Cairo.ttf");
                        TextView text_chat = (TextView) toolbar.findViewById(R.id.text_toolbar);
                        text_chat.setText("شات");
                        text_chat.setTypeface(typeface);
                        fragment = new Chat_Fragment();
                        setFragemnt(fragment);
      */
                    startActivity(new Intent(getApplicationContext() , Sign_With_Google.class));
                    /**
                     * I have 3 fragment in navigation bottom , I want to make activity(Sign_With_Google) to enable user to register
                     * using Google Sign in , but this activity will appear when i click to chat icon(chat fragment) in navigation bottom
                     * if user registered , chat fragment appear automatically , if user not registered , the activity will appear to enable user to register
                     * i'm sorry if my words are not clear i'm not good at english
                     * */

                        return true;

                }
                return false;
            }
        };


private void setFragemnt(Fragment fragemnt) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.frame_container, fragemnt);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();


}

> Sign_With_Google





import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.google.android.gms.common.SignInButton;

public class Sign_With_Google extends AppCompatActivity {

    SignInButton signInButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign__with__google);
        signInButton = findViewById(R.id.btn_Sign_In);
        signInButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

              getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.frame_container , new Chat_Fragment()).commit();


            }
        });

    }
}

activity_main.xml activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/app_bar_main">




            <FrameLayout
                android:layout_above="@id/bottom_navigation"
                android:id="@+id/frame_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent">


            </FrameLayout>



    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/bottom_navigation_menu" />


</RelativeLayout>

Sign_With_Google.xml Sign_With_Google.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Sign_With_Google">

    <com.google.android.gms.common.SignInButton

        android:id="@+id/btn_Sign_In"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginTop="271dp">

    </com.google.android.gms.common.SignInButton>


</RelativeLayout>

Chat_Fragment Chat_Fragment

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class Chat_Fragment extends Fragment {

    public static FragmentManager fragmentManager;

    public Chat_Fragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);

        fragmentManager = getActivity().getSupportFragmentManager();

        if (getActivity().findViewById(R.id.frame_container) != null) {
            if (savedInstanceState != null) {

                return;

            }

            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            Chat_Fragment chat_fragment = new Chat_Fragment();
            fragmentTransaction.replace(R.id.frame_container, chat_fragment, null).commit();

        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_chat_, container, false);




        return view;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.chat_menu, menu);
        MenuItem menuItem = menu.findItem(R.id.signout);
    }
}

Home_Fragment Home_Fragment

public Home_Fragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_, container, false);
        setHasOptionsMenu(true);

        return view;
    }

Section_Fragment Section_Fragment

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 */
public class Section_Fragment extends Fragment {


    public Section_Fragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_section_, container, false);
    }

}

You could use onActivityResult . 您可以使用onActivityResult

In some place of MainActivity , add below lines: MainActivity某个地方,添加以下行:

Intent intent=new Intent(MainActivity.this,SecondActivity.class);  
startActivityForResult(intent, 2);// Activity is started with requestCode 2  

And override onActivityResult method: 并重写onActivityResult方法:

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

    if(requestCode==2)  
    {  
        boolean ifRegistered = data.getBoolean("registered", false);  
        if(ifRegistered){
            //open chat fragment 
        }else{
            //do something
        } 
    } 

 }

And in some place of second activity: 在第二个活动的某个地方:

Intent intent=new Intent();  
intent.putExtra("registered",true);  
setResult(2,intent);  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从活动返回到导航底部的特定片段 - How to return from activity to a specific fragment in navigation bottom 如何使用底部导航活动更改片段? - How to change fragment with the Bottom Navigation Activity? 通过底部导航视图通过活动将价值从一个片段传递到另一个片段 - Pass value from fragment to fragment through activity with bottom navigation view 将底部导航活动转换为片段 - Convert Bottom Navigation Activity to Fragment Kotlin:使用 Kotlin 提供的标准底部导航活动时如何将数据从 MainActivity 传递到片段 - Kotlin: How to pass data from the MainActivity to a fragment when working with the standard bottom navigation activity provided by Kotlin 如何实现底部导航栏事务不是碎片,而是活动 class? - How to Implementing bottom navigation bar transaction not to fragment , however to Activity class? 从其他活动返回特定片段 - Return to a specific Fragment from a different Activity 如何从底部工作表片段开始活动 - How to start activity from bottom sheet fragment 如何从导航抽屉的片段中启动活动 - how to launch an activity from a fragment of a navigation drawer 如何从底部工作表导航片段移动到其他片段 - How to move to other fragment from Bottom Sheet Navigation Fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM