Search code examples
javaandroidandroid-fragmentsandroid-activityandroid-recyclerview

How to call a Fragment from activity. When the view doesn't belong to corresponding layout


In my scenario, I've a fragmemt where there are list of options(Recycler view) that a user can select. Example: my profile, change password, logout.

If the user clicks the change password from the Fragment A recycler view adapter, then the user is navigated to Activity A. Where the user can change the password. once submit button is clicked in Activity A then user should be navigated to the Fragment A.

Code in Activity A

package com.example.expresso.sapthagiri.yogirproduct;

import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ChangePassword extends AppCompatActivity {

    Button submit, back;
    EditText old_password, new_password, re_enter_password;

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

        submit = findViewById(R.id.submit);
        back = findViewById(R.id.backButton);
        old_password = findViewById(R.id.oldPassword);
        new_password = findViewById(R.id.newPassword);
        re_enter_password = findViewById(R.id.reEnterPassword);

        final ProfileFragment profileFragment = new ProfileFragment();

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setFragment(profileFragment);
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setFragment(profileFragment);
            }
        });

    }

    private void setFragment(android.support.v4.app.Fragment fragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.main_frame, fragment).commit();
    }
}

Actual issue as of now: I'm trying to load the fragment A but R.id.main_frame doesn't belong to setContentView(R.layout.activity_change_password);.

private void setFragment(android.support.v4.app.Fragment fragment) {
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.main_frame, fragment).commit();
    }

Kindly help me overcome to issue. Million thanks in advance! :)


Solution

  • You are confusing the navigation architecture of android. When you start Activity A it adds Activity A to the activity stack. Therefore the Activity with Fragment A is still in that stack of activities. So all you have to do is finish the activity A. Which will basically do a pop operation on that activity stack.

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed(); //or finish()
            }
        });
    

    This will do the trick.