Creating a child intent from a fragment

I am using FragmentActivity to switch between Fragment . But I would like to have an admin button on a fragment, and when I click on it, the new fragment or activity is shown as a child (with a back button in the action bar).

How can i do this?

Here is my code that works, but the back button is not showing in line:

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 a snippet 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 need to change something in the manifest?

+3


source to share


3 answers


U needs to be overridden onCreateOptionsMenu

and onOptionsItemSelected

. In the method, onCreateOptionsMenu

do the following: Populate the menu in the action bar. You can define the contents of a menu item in a folder res/menu

.



Further down in the method, onOptionsItemSelected

you can handle clicks of the Back button added to the action bar. Also keep one thing in mind. In your manifest, please use a theme that has an action bar. Example: in the app tag, use android:theme="@android:style/Theme.Light"

and not something likeandroid:theme="@android:style/Theme.Light.NoTitleBar

+1


source


All you have to do is turn it into the activity you are currently in.

Once inside FragmentActivity

: getActionBar().setHomeAsUpEnabled(boolean)

.



Otherwise inside a Fragment

: getActivity().getActionBar().setHomeAsUpEnabled(boolean)

.

+1


source


Well if you are starting new Activity

out you can enable the button in it by writing shouldDisplayHomeUp();

in the method onCreate()

and on the back you have to go to the previous action in the back. And in another case of adding, new Fragment

you can take a look at this answer for reference, as it mentions that when new Fragment

you add, you add it to back stack

like this

getSupportFragmentManager().beginTransaction()
                           .add(detailFragment, "detail")
                           // Add this transaction to the back stack
                           .addToBackStack()
                           .commit();

      

this will cause the Back button to take you to the previous Fragment

+1


source







All Articles