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

时间:2018-09-11 22:09:32

标签: java android android-studio

  

说明

我在导航底部有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

<?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

<?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

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

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

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

}

1 个答案:

答案 0 :(得分:0)

您可以使用onActivityResult

MainActivity的某个地方,添加以下几行:

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

并覆盖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
        } 
    } 

 }

在第二个活动的某个地方:

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