Reputation: 13189
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!
Upvotes: 0
Views: 1099
Reputation: 43
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
Upvotes: 1
Reputation: 226
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);
Upvotes: 0
Reputation: 13189
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.
Upvotes: 0
Reputation: 5370
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
Upvotes: 1