3

I'm using FragmentActivity for switching between Fragment. But I would like to have a Admin Button on a fragment, and when I click on it, a new fragment or activity appears like a child (with the back button in action bar).

How can I make it ?

Here is my code, that works, but the back button doesn't appear in 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 the moment... but maybe Fragment if we need ?) :

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 Manifest ?

xcode_Dev
  • 237
  • 4
  • 16

3 Answers3

1

U need to override the onCreateOptionsMenu and onOptionsItemSelected. In the onCreateOptionsMenu method do the following : Inflate the menu into the action bar. You can define the contents of the menu item under res/menu folder.

Next in the onOptionsItemSelected method, you can handle the clicks of the back button added in the action bar. Also keep in mind one thing. In the manifest please use a theme which has action bar in it. Example : Under the application tag use android:theme="@android:style/Theme.Light" and not anything like android:theme="@android:style/Theme.Light.NoTitleBar

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
XylemRaj
  • 772
  • 4
  • 13
  • 28
1

All you need to do is enable it inside the activity you're currently at.

When inside a FragmentActivity: getActionBar().setHomeAsUpEnabled(boolean).

Otherwise, inside a Fragment: getActivity().getActionBar().setHomeAsUpEnabled(boolean).

VulfCompressor
  • 1,390
  • 1
  • 11
  • 26
  • getSupportActionBar().setDisplayHomeAsUpEnabled(true); works fine, the back button appears, but nothing happen when I tap on it... – xcode_Dev Apr 28 '15 at 14:21
  • Oh. Almost forgot: your activity in the manifest must have the attribute `android:parentActivityName` declared, and its value must be the full canonical name of the Activity class. More info: [Android Developers - ](http://developer.android.com/guide/topics/manifest/activity-element.html) – VulfCompressor Apr 28 '15 at 14:42
1

Well if you are starting a new Activity you can enable the back button in it by writing shouldDisplayHomeUp(); in the onCreate() method and on back should take you to the previous activity in the back stack. And in the other case of adding a new Fragment you can take a look on this answer for reference as it mentions that when you add a new Fragment you add it to the back stack like this

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

this will make the back button take you to your previous Fragment

Community
  • 1
  • 1
M090009
  • 1,129
  • 11
  • 20
  • getSupportActionBar().setDisplayHomeAsUpEnabled(true); works fine, the back button appears, but nothing happen when I tap on it... – xcode_Dev Apr 28 '15 at 14:19
  • In the Activity you can either override the onBackPressed() and go back to your fragment activity, or you can set your main Activity that has the initial fragment to be the parent in your manifest – M090009 Apr 28 '15 at 14:28
  • I don't really understand, I just added getSupportActionBar().setDisplayHomeAsUpEnabled(true); in my Activity for displaying back button. But when I tap on the back button I would like that back to my fragment. How can I make that ? thanks man for your help...! – xcode_Dev Apr 28 '15 at 14:33
  • Alright, I added this : public boolean onOptionsItemSelected(MenuItem item) { finish(); return super.onOptionsItemSelected(item); } – xcode_Dev Apr 28 '15 at 14:39
  • You can override the ActionBar back button by adding "case android.R.id.home:" and as @Rodolfo Perottoni mentioned you should also add "android:parentActivityName="com.example.YOUR APP.your FragmentActivity" >" to your new activity manifest – M090009 Apr 28 '15 at 14:51