android - 不能使用if else条件与communicator

时间:2016-06-30 23:17:37

标签: java android android-fragments fragment bundle

我有一个大约10个片段的应用程序,我做了一个传递信息传递。现在,当我运行应用程序时,我的主要活动已经实现了所有通信器,因为我实现了Communicator接口。现在在oncreate中我的主要活动我想在通信器中使用if方法的其他功能,并且发生的是所有6个方法正在创建,或者正在创建实例......这不是我想要的。当我在片段中使用我的后退按钮时,我注意到了它,我不得不按下后退按钮6次才能离开。是否有修复,请帮助。

Communicator界面:

public interface Communicator {
public void requestQuestionsId1(String data);
public void requestQuestionsId2(String data);
public void requestQuestionsId3(String data);
public void requestQuestionsId4(String data);
public void requestQuestionsId5(String data);
public void requestQuestionsId6(String data);
public void show(int data);
}

主要Activity.java

public class MainActivity extends AppCompatActivity  implements NavigationView.OnNavigationItemSelectedListener,
        Communicator,
        QuestionOne.OnFragmentInteractionListener,
        QuestionTwo.OnFragmentInteractionListener,
        QuestionThree.OnFragmentInteractionListener,
        QuestionFour.OnFragmentInteractionListener,
        QuestionFive.OnFragmentInteractionListener,
        QuestionSix.OnFragmentInteractionListener,
        QuestionSeven.OnFragmentInteractionListener,
        QuestionEight.OnFragmentInteractionListener,
        QuestionNine.OnFragmentInteractionListener,
        QuestionTen.OnFragmentInteractionListener
        {
    private static final String TAG = "MainActivity";


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


        int ageValue = Integer.parseInt(newAge);
        //Log.d("MainActivity-AgeValue: ", String.valueOf(ageValue));
        if(requestPossibilityId(ageValue, newSex, newBelizean) == 1){
            //documents.add(2 photos(2x2 inches)
            //documents.add(application form)
            //documents.add(Form 4a)
            //documents.add(Form 4b)
            Toast.makeText(this, "Possibility 1", Toast.LENGTH_SHORT).show();
            requestQuestionsId1("Minor Belizean");

        }else if(requestPossibilityId(ageValue, newSex, newBelizean) == 2){
            //documents.add(2 photos(2x2 inches)
            //documents.add(application form)
            //documents.add(Form 4a)
            //documents.add(Form 4b)
            Toast.makeText(this, "Possibility 2", Toast.LENGTH_SHORT).show();
            requestQuestionsId2("Minor Non Belizean");

        }else if(requestPossibilityId(ageValue, newSex, newBelizean) == 3){
            //documents.add(2 photos(2x2 inches)
            //documents.add(application form)
            //documents.add(Form 4a)
            //documents.add(Form 4b)
            Toast.makeText(this, "Possibility 3", Toast.LENGTH_SHORT).show();
            requestQuestionsId3("Adult Male Belizean");

        }else if(requestPossibilityId(ageValue, newSex, newBelizean) == 4){
            //documents.add(2 photos(2x2 inches)
            //documents.add(application form)
            //documents.add(Form 4a)
            //documents.add(Form 4b)
            Toast.makeText(this, "Possibility 4", Toast.LENGTH_SHORT).show();
            requestQuestionsId4("Adult Male Non Belizean");

        }else if(requestPossibilityId(ageValue, newSex, newBelizean) == 5){
            //documents.add(2 photos(2x2 inches)
            //documents.add(application form)
            //documents.add(Form 4a)
            //documents.add(Form 4b)
            Toast.makeText(this, "Possibility 5", Toast.LENGTH_SHORT).show();
            requestQuestionsId5("Adult Female Belizean");

        }else if(requestPossibilityId(ageValue, newSex, newBelizean) == 6){
            //documents.add(2 photos(2x2 inches)
            //documents.add(application form)
            //documents.add(Form 4a)
            //documents.add(Form 4b)
            Toast.makeText(this, "Possibility 6", Toast.LENGTH_SHORT).show();
            requestQuestionsId6("Adult Female Non Belizean");

        }else
            Toast.makeText(this, "Error, could not load user informatio", Toast.LENGTH_SHORT).show();
        //getPersonalInfo(ageValue, newSex, newBelizean);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_item_home) {
            Bundle bundle = new Bundle();
            String myMessage = "Stackoverflow is cool!";
            bundle.putString("message", myMessage );
            QuestionOne questionOne = new QuestionOne();
            questionOne.setArguments(bundle);
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.containerView, questionOne, questionOne.getTag()).commit();
        } else if (id == R.id.nav_item_profile) {

        } else if (id == R.id.nav_item_location) {
            Intent intent = new Intent(MainActivity.this, MapsActivity.class);
            startActivity(intent);
        } else if (id == R.id.nav_item_about_and_help) {
            AboutAndHelp aboutAndHelp= new AboutAndHelp();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.containerView, aboutAndHelp, aboutAndHelp.getTag()).commit();
        } else if (id == R.id.nav_item_office_info) {
            ContactFragment contactFragment= new ContactFragment();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.containerView, contactFragment, contactFragment.getTag()).commit();
        } else if (id == R.id.nav_item_contact_us) {
            UserSubmitForm userSubmitForm= new UserSubmitForm();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.containerView, userSubmitForm, userSubmitForm.getTag()).commit();
        }

    }

    @Override
    public void requestQuestionsId1(String data) {
/*        Bundle bundle = new Bundle();
        bundle.putString("message", data );*/
        if(Objects.equals(data, "Minor Belizean")){
            QuestionOne questionOne = new QuestionOne();
            FragmentManager firstManager = getSupportFragmentManager();
            firstManager.beginTransaction().replace(R.id.containerView, questionOne, questionOne.getTag()).commit();
        }if(Objects.equals(data, "questionOneYes")){
            QuestionFive questionFive = new QuestionFive();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.containerView, questionFive, questionFive.getTag()).addToBackStack(null).commit();
        }if(Objects.equals(data, "questionOneNo")){
            QuestionTwo questionTwo = new QuestionTwo();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.containerView, questionTwo, questionTwo.getTag()).addToBackStack(null).commit();
        }if(Objects.equals(data, "questionTwoYes")){
            //documents.add(police report)
            //documents.add(form 6)
            QuestionFive questionFive = new QuestionFive();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.containerView, questionFive, questionFive.getTag()).addToBackStack(null).commit();
        }if(Objects.equals(data, "questionTwoNo")){
            //documents.add(previous passport)
            QuestionThree questionThree = new QuestionThree();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.containerView, questionThree, questionThree.getTag()).addToBackStack(null).commit();
        }if(Objects.equals(data, "questionThreeSeverelyDamaged")){
            //documents.add(police report)
            //documents.add(form 6)
            QuestionFive questionFive = new QuestionFive();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.containerView, questionFive, questionFive.getTag()).addToBackStack(null).commit();
        }if(Objects.equals(data, "questionThreePartiallyDamaged")){
            //documents.add(form 6)
            QuestionFive questionFive = new QuestionFive();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.containerView, questionFive, questionFive.getTag()).addToBackStack(null).commit();
        }if(Objects.equals(data, "questionThreeNotDamaged")){
            QuestionFour questionFour = new QuestionFour();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.containerView, questionFour, questionFour.getTag()).addToBackStack(null).commit();
        }if(Objects.equals(data, "questionFourYes")){
            QuestionFive questionFive = new QuestionFive();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.containerView, questionFive, questionFive.getTag()).addToBackStack(null).commit();
        }if(Objects.equals(data, "questionFourNo")){
            QuestionFive questionFive = new QuestionFive();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.containerView, questionFive, questionFive.getTag()).addToBackStack(null).commit();
        }if(Objects.equals(data, "questionFiveAdoption")){
            //documents.add(adoption certificates)
            QuestionSix questionSix = new QuestionSix();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.containerView, questionSix, questionSix.getTag()).addToBackStack(null).commit();
        }if(Objects.equals(data, "questionFiveVitalBirth")){
            //documents.add(birth certificates)
            QuestionSix questionSix = new QuestionSix();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.containerView, questionSix, questionSix.getTag()).addToBackStack(null).commit();
        }if(Objects.equals(data, "questionSixParent")){
            //documents.add(parent ID)
            QuestionNine questionNine = new QuestionNine();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.containerView, questionNine, questionNine.getTag()).addToBackStack(null).commit();
        }if(Objects.equals(data, "questionSixGuardian")){
            //documents.add(legal guardianship)
            //documents.add(guardianship id)
            QuestionNine questionNine = new QuestionNine();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.containerView, questionNine, questionNine.getTag()).addToBackStack(null).commit();
        }if(Objects.equals(data, "questionSixNone")){
            Toast.makeText(this, "Cannot Apply", Toast.LENGTH_SHORT).show();
        }if(Objects.equals(data, "questionNineYes")){
            //documents.add(deed poll required)
            QuestionTen questionTen = new QuestionTen();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.containerView, questionTen, questionTen.getTag()).addToBackStack(null).commit();
        }if(Objects.equals(data, "questionNineNo")){
            QuestionTen questionTen = new QuestionTen();
            FragmentManager manager = getSupportFragmentManager();
            manager.beginTransaction().replace(R.id.containerView, questionTen, questionTen.getTag()).addToBackStack(null).commit();
        }if(Objects.equals(data, "questionTenYes")){
            //documents.add(medical papers)
            Toast.makeText(this, "End of the app", Toast.LENGTH_SHORT).show();
        }if(Objects.equals(data, "questionTenNo")){
            Toast.makeText(this, "End of the app", Toast.LENGTH_SHORT).show();
        }
/*        QuestionTwo questionTwo = new QuestionTwo();
        questionTwo.setArguments(bundle);
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.containerView, questionTwo, questionTwo.getTag()).addToBackStack(null).commit();
        Toast.makeText(this, data, Toast.LENGTH_SHORT).show();*/


    }

    @Override
    public void onFragmentInteraction(Uri uri) {
        Toast.makeText(this, (CharSequence) uri, Toast.LENGTH_SHORT).show();

    }
    public int requestPossibilityId(int age, String sex, String nationality){
        Integer requestQuestionId = 0;
        if(age < 16 && Objects.equals(nationality, "Belizean")){
            requestQuestionId = 1;
            Log.d("MainActivity", "Under 16 Belizean");
        }if(age < 16 && Objects.equals(nationality, "Non Belizean")){
            requestQuestionId = 2;
            Log.d("MainActivity", "Under 16 Non Belizean");
        }if(age >= 16 && Objects.equals(sex, "Male") && Objects.equals(nationality, "Belizean")){
            requestQuestionId = 3;
            Log.d("MainActivity", "Adult Male Belizean");
        }if(age >= 16 && Objects.equals(sex, "Male") && Objects.equals(nationality, "Non Belizean")){
            requestQuestionId = 4;
            Log.d("MainActivity", "Adult Male Non Belizean");
        }if(age >= 16 && Objects.equals(sex, "Female") && Objects.equals(nationality, "Belizean")){
            requestQuestionId = 5;
            Log.d("MainActivity", "Adult Female Belizean");
        }if(age >= 16 && Objects.equals(sex, "Female") && Objects.equals(nationality, "Non Belizean")){
            requestQuestionId = 6;
            Log.d("MainActivity", "Adult Female Non Belizean");
        }
        Log.d("MainActivity-Id: ", String.valueOf(requestQuestionId));
        return requestQuestionId;
    }


}

我取消了5个遗失的requestQuestionsId2,3,4,5(字符串数据),因为SO不会让我把它;它基本上与requestQuestionId1

具有相同的代码

1 个答案:

答案 0 :(得分:0)

您正在运行的每个片段事务都使用addToBackStack()。即使您使用replace(),这仍会导航到以前的片段。删除addToBackStack()方法,它不会将片段放在后面的堆栈上。

另外,请注意,您正在将很多if语句链接在一起。我想你可能真的想要if() {} else if() {}块。