Java – Create child intents from fragments

Create child intents from fragments… here is a solution to the problem.

Create child intents from fragments

I’m using FragmentActivity to switch between Fragments. But I want to have a manage button on a fragment, and when I click on it, a new fragment or activity looks like a child (using the back button in the action bar).

How can I do it?

Here is my code that works, but the back button doesn’t appear in the action bar:

fragment :

public class Reports extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (container == null) {
            return null;
        }
public void onClick(View v) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                getActivity().startActivity(intent);
            }
        });
    }
}

Activity (for now… But maybe Fragment if we need to? ):

public class LoginActivity extends ActionBarActivity {
    public static final String TAG = LoginActivity.class.getSimpleName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        Button loginButton = (Button) findViewById(R.id.loginButton);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView emailText = (TextView) findViewById(R.id.emailText);
                TextView passwordText = (TextView) findViewById(R.id.passwordText);
                ParseUser.logInInBackground(emailText.getText().toString(), passwordText.getText().toString(), new LogInCallback() {
                    public void done(ParseUser user, ParseException e) {
                        if (user != null) {
                            Log.i(TAG, "Yeahhh Login OK");
                            finish();
                        } else {
                            runOnUiThread();
                        }
                    }
                });
            }
        });
    }

Maybe I have to change something in the manifest?

Solution

All you need to do is enable it in the activity you are currently in.

In FragmentActivity: getActionBar().setHomeAsUpEnabled(boolean).

Otherwise, in the fragment: getActivity().getActionBar().setHomeAsUpEnabled(boolean).

Related Problems and Solutions