从活动2片段返回到活动1片段

时间:2016-07-29 04:15:43

标签: android android-fragments

我有2个活动 - MainActivity和MainActivity2。

MainActivity有2个片段 MainActivity2有1个片段

所以我将数据从MainActivity_Frag_one传递给MainActivity_Frag_two。 当我点击MainActivity_Frag_two中的设备后退按钮和蓝色按钮时,返回MainActivity_Frag_one可以很好地工作。

之后,我使用intent将数据从MainActivity_Frag_two传递给MainActivity2_Frag_one

当我点击设备返回按钮时,返回MainActivity_Frag_two可以很好地工作但是蓝色按钮不起作用(点击时没有效果,也没有显示错误)。

我可以知道哪个部分出了问题吗?

enter image description here MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager manager=getSupportFragmentManager();
    FragmentTransaction transaction=manager.beginTransaction();
    MainActivity_Frag_one MainActivity_Frag_one=new MainActivity_Frag_one();
    transaction.add(R.id.activity_one_container,MainActivity_Frag_one);
    transaction.commit();
}

MainActivity_Frag_one.java

public class MainActivity_Frag_one extends Fragment {

EditText get_text;
Button get_button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_main_activity__frag_one,container,false);

    get_text=(EditText)rootView.findViewById(R.id.input_name);
    get_button=(Button)rootView.findViewById(R.id.submit_button);

    get_button.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){


            FragmentManager manager=getFragmentManager();
            FragmentTransaction transaction=manager.beginTransaction();
            Bundle bundle=new Bundle();
            bundle.putString("message", get_text.getText().toString());
            MainActivity_Frag_two fragobj=new MainActivity_Frag_two();
            fragobj.setArguments(bundle);
            transaction.replace(R.id.activity_one_container,fragobj);
            transaction.addToBackStack("zzz");
            transaction.commit();

        }

    });

    return rootView;
}
}

MainActivity_Frag_two.java

public class MainActivity_Frag_two extends Fragment {

ImageView get_button;
Button get_to_second_activity;
String strtext;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_main_activity__frag_two,container,false);
    TextView display_text=(TextView)rootView.findViewById(R.id.display_text);
    strtext=getArguments().getString("message");
    display_text.setText(strtext);

    get_button=(ImageView)rootView.findViewById(R.id.back_button);
    get_button.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){

            getFragmentManager().popBackStack();

        }
    });

    get_to_second_activity=(Button)rootView.findViewById(R.id.to_second_activity);
    get_to_second_activity.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){
            Intent intent = new Intent(getActivity(),MainActivity2.class);
            intent.putExtra("Check","data from mainactivity_frag_two is : "+strtext);
            startActivity(intent);

            FragmentManager manager=getFragmentManager();
            FragmentTransaction transaction=manager.beginTransaction();
            transaction.addToBackStack("");
            transaction.commit();
        }
    });
    return rootView;
}
}

MainActivity2.java

public class MainActivity2 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    FragmentManager manager=getSupportFragmentManager();
    FragmentTransaction transaction=manager.beginTransaction();
    MainActivity2_Frag_one MainActivity2_Frag_one=new MainActivity2_Frag_one();
    transaction.add(R.id.activity_two_container,MainActivity2_Frag_one);
    transaction.commit();

}
}

MainActivity2_Frag_one.java

public class MainActivity2_Frag_one extends Fragment {
TextView get_display;
ImageView get_button;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_main_activity2__frag_one,container,false);

    get_display=(TextView)rootView.findViewById(R.id.display);
    String data1=getActivity().getIntent().getExtras().getString("Check");
    get_display.setText(data1);

    get_button=(ImageView)rootView.findViewById(R.id.back_to_previous_frag);
    get_button.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){

            getFragmentManager().popBackStack();

        }
    });
    return rootView;
}
}

1 个答案:

答案 0 :(得分:0)

要返回上一个活动,您不需要从后台堆栈中弹出片段。你可以调用finish()。

在您的MainActivity2_Frag_one.java

  

替换它:

get_button.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){

            getFragmentManager().popBackStack();

        }
    });
  

get_button.setOnClickListener(new View.OnClickListener(){

        public void onClick(View v){

            getActivity().finish();

        }
    });

原因是popBackStack()可以在同一个活动中使用。