0

I would like to start an Activity from a Fragment when I press a button on the last one but I am getting a blank layout instead of the layout that is associated to the Activity.

I call the Fragment from MainActivity.class like this:

FragmentManager mFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.mainContent,new FragmentMain()).commit();

And on FragmentMain class I have an onClick method like follows:

@Override
public void onClick(View v) {
    Intent intent = new Intent(getActivity(), FinalClass.class);
    getActivity().startActivity(intent);
}

but the result is a blank layout and no errors are shown on the logcat. I have searched on the web for similar behaviours and I found some solutions that did not resolve my problem:

I do not have any method addToBackStack() on my project so this solution does not work for me.

getActivity().startActivity(myIntent) is the accepted answer.

The ways that they use to start the Activity from the Fragment does not change from the way I am using.

Any of the solutions provided worked for me.

P.S: I have declared the Activity on the Manifest.xml file:

<activity
      android:name=".FinalClass"
      android:screenOrientation="portrait"
      android:label="Final Class"
      android:icon="@mipmap/ic_launcher"
      android:theme="@style/AppTheme.NoActionBar" />

P.S.2: Do not doubt to ask if you need more code (I tried to simplify it to focus on the problem).

What can I do to avoid that the Activity gives to me a blank layout instead of the layout associated to that Activity?

Thanks in advance!

Community
  • 1
  • 1
Francisco Romero
  • 12,787
  • 22
  • 92
  • 167
  • do have any views in the layout of `FInalClass` activity? – Burhanuddin Rashid Aug 12 '16 at 09:54
  • Just a side note, not sure if doing 'getActivity().startActivity()' from a fragment is a good practice. I'd suggest using an interface that your activity will implement, then call a method on the interface from your fragment. Then start your activity from your activity. Don't think it's related to your problem though.. – vkislicins Aug 12 '16 at 10:03
  • @BurhanuddinRashid I have the following: `public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { final View inflaterFinalClass = inflater.inflate(R.layout.activity_final_class, null); return inflaterFinalClass;` – Francisco Romero Aug 12 '16 at 10:05
  • 1
    `FinalClass` is Fragment?..show layout code for `activity_final_class` – Burhanuddin Rashid Aug 12 '16 at 10:09
  • @vkislicins I suppose that you would want to put: "Then start your activity from your interface". Why do you think is not a good practice? (before I had only `startActivity` instead of `getActivity().startActivity`, I have changed it as suggested on the questions related. – Francisco Romero Aug 12 '16 at 10:10
  • @BurhanuddinRashid No, it is an Activity. – Francisco Romero Aug 12 '16 at 10:10
  • 1
    @Error404 Your activity implements your interface, so by calling a method on your interface you are effectively calling a method in your activity, which will in turn start another activity. Hence starting activity from activity. I think it's cleaner that way, activity changes are at the top level, easier to maintain the code. Also fragments are effectively children of the activity and exist within the activity, so by starting an activity from fragment it's like a child ordering a parent :) – vkislicins Aug 12 '16 at 10:22
  • @vkislicins Thank you for the explanation! – Francisco Romero Aug 12 '16 at 10:28

4 Answers4

1

Set your layout in activity like this in onCreate

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

dont use onCreateView for activity

Burhanuddin Rashid
  • 5,260
  • 6
  • 34
  • 51
1

You cannot start a activity from Fragment using fragment instance.but you can start a activity using fragment container activity instance. For eg: Let say Activity A having Fragment Abc, so you have to use the instance of Activity A not Fragment abc.

By doing this there will be no effect in your previous fragment ,because it seat top of your Activity, instead of replacing or adding existing Fragment. If you want do some changes in existing Fragment then you have to override onActivityResult

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    } 

:)GlbMP

0

You can use onCreateView method then use this code it will also help...

Intent intent = new Intent(getActivity(), FinalClass.class);
getActivity().startActivity(intent);

// Closing all the Activities
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

// Add new Flag to start new Activity
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Sikander Bhutto
  • 226
  • 1
  • 11
0

Finally, I found the solution. I was using the wrong onCreate method.

Before, I had:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
       final View inflaterFinalClass = inflater.inflate(R.layout.activity_final_class, null); 
       return inflaterFinalClass;

but I should use:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_final_class);
}

So, in conclusion, I was using the onCreate method for a Fragment instead of the onCreate method for an Activity.

Francisco Romero
  • 12,787
  • 22
  • 92
  • 167