Add fragment to an activity












-1














I'm having trouble adding a fragment to an activity.



When i open the app in Landscape mode the fragment is working as it should and displays the infos next to the list.
But when i try to display infos in portrait mode the app starts a new activity but doesnt display the fragment.



The activity that is opened with the fragment is empty.
The App doesn't even go into the function onCreateView because the Log.d(TAG, "onCreateView: ciao") is never executed.



Here is the Java code:



public class ActivityMain extends AppCompatActivity {
private static final String TAG = "ActivityMain";



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: started");

LeftFragment leftFragment = new LeftFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.mainLayout, leftFragment).commit();
}


}



public class LeftFragment extends Fragment {
private static final String TAG = "LeftFragment";

public LeftFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView: ciao");
View view = inflater.inflate(R.layout.frag, container, false);

ListView listView = (ListView) view.findViewById(R.id.list2);

/* 100 Zufallszahlen in ArrayList "names" schreiben */
Random rand = new Random();
int n = rand.nextInt(200)+1;
for(int i = 0; i <= 100; i++)
{
n = rand.nextInt(200)+1;
DataContainer.names.add(Integer.toString(n));
}

/* Arrayadapter initialisieren, der Adapter holt sich seine Daten aus der ihm zugewiesenen Liste "names"
die listview bekommt einen Adapter zugewiesen, der sie mit den Daten aus der Liste versorgt
*/
ArrayAdapter adapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, DataContainer.names);
listView.setAdapter(adapter);
/* Onclick Listener für die Listenelemente */
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedData = DataContainer.names.get(position);
DetailsFragment detailsFragment = new DetailsFragment();

Bundle bundle = new Bundle();
bundle.putString("selected_data", selectedData);

detailsFragment.setArguments(bundle);

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
Intent intent;
intent = new Intent(getActivity(), Activity_second.class);
Log.w(TAG, "onItemClick: " + intent);
intent.putExtra("selected_data", selectedData);
startActivity(intent);
}
else
{
FragmentManager manager = getFragmentManager();
manager.beginTransaction().replace(R.id.detailLayout, detailsFragment).commit();
}
}
});
return view;
}


public static class DataContainer{
static ArrayList<String> names = new ArrayList<>();
//static ArrayList<String> details = new ArrayList<>();
}


}



    public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_details, container, false);
TextView TextDetailHeadline = (TextView)view.findViewById(R.id.TextDetailHeadline);
TextView TextDetails = (TextView)view.findViewById(R.id.TextDetails);
Bundle bundle = getArguments();

if(bundle != null) {
if (bundle.getString("selected_data") != null){
TextDetailHeadline.setText(bundle.getString("selected_data"));
TextDetails.setText(bundle.getString("selected_data"));
}
}
return view;
}


}



public class Activity_second extends AppCompatActivity {

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

FragmentManager manager = getSupportFragmentManager();
DetailsFragment detailsFragment = new DetailsFragment();
detailsFragment.setArguments(getIntent().getExtras());
manager.beginTransaction().replace(R.id.detailLayout, detailsFragment).commit();
}


}



Found the error by myself: my Activity_Second linked to the wrong XML file in line:



    setContentView(R.layout.activity_details);


..Yikes










share|improve this question
























  • Please post the portrait activity_main layout.
    – Mike M.
    Nov 23 at 0:10










  • Please post your activity_main especially if you have custom activity_main for portrait, and post your manifest
    – Momen Zaqout
    Nov 23 at 0:30










  • Found the error and edited my post with the correct code. Thanks to you two!
    – Fleet23
    Nov 23 at 18:06
















-1














I'm having trouble adding a fragment to an activity.



When i open the app in Landscape mode the fragment is working as it should and displays the infos next to the list.
But when i try to display infos in portrait mode the app starts a new activity but doesnt display the fragment.



The activity that is opened with the fragment is empty.
The App doesn't even go into the function onCreateView because the Log.d(TAG, "onCreateView: ciao") is never executed.



Here is the Java code:



public class ActivityMain extends AppCompatActivity {
private static final String TAG = "ActivityMain";



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: started");

LeftFragment leftFragment = new LeftFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.mainLayout, leftFragment).commit();
}


}



public class LeftFragment extends Fragment {
private static final String TAG = "LeftFragment";

public LeftFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView: ciao");
View view = inflater.inflate(R.layout.frag, container, false);

ListView listView = (ListView) view.findViewById(R.id.list2);

/* 100 Zufallszahlen in ArrayList "names" schreiben */
Random rand = new Random();
int n = rand.nextInt(200)+1;
for(int i = 0; i <= 100; i++)
{
n = rand.nextInt(200)+1;
DataContainer.names.add(Integer.toString(n));
}

/* Arrayadapter initialisieren, der Adapter holt sich seine Daten aus der ihm zugewiesenen Liste "names"
die listview bekommt einen Adapter zugewiesen, der sie mit den Daten aus der Liste versorgt
*/
ArrayAdapter adapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, DataContainer.names);
listView.setAdapter(adapter);
/* Onclick Listener für die Listenelemente */
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedData = DataContainer.names.get(position);
DetailsFragment detailsFragment = new DetailsFragment();

Bundle bundle = new Bundle();
bundle.putString("selected_data", selectedData);

detailsFragment.setArguments(bundle);

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
Intent intent;
intent = new Intent(getActivity(), Activity_second.class);
Log.w(TAG, "onItemClick: " + intent);
intent.putExtra("selected_data", selectedData);
startActivity(intent);
}
else
{
FragmentManager manager = getFragmentManager();
manager.beginTransaction().replace(R.id.detailLayout, detailsFragment).commit();
}
}
});
return view;
}


public static class DataContainer{
static ArrayList<String> names = new ArrayList<>();
//static ArrayList<String> details = new ArrayList<>();
}


}



    public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_details, container, false);
TextView TextDetailHeadline = (TextView)view.findViewById(R.id.TextDetailHeadline);
TextView TextDetails = (TextView)view.findViewById(R.id.TextDetails);
Bundle bundle = getArguments();

if(bundle != null) {
if (bundle.getString("selected_data") != null){
TextDetailHeadline.setText(bundle.getString("selected_data"));
TextDetails.setText(bundle.getString("selected_data"));
}
}
return view;
}


}



public class Activity_second extends AppCompatActivity {

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

FragmentManager manager = getSupportFragmentManager();
DetailsFragment detailsFragment = new DetailsFragment();
detailsFragment.setArguments(getIntent().getExtras());
manager.beginTransaction().replace(R.id.detailLayout, detailsFragment).commit();
}


}



Found the error by myself: my Activity_Second linked to the wrong XML file in line:



    setContentView(R.layout.activity_details);


..Yikes










share|improve this question
























  • Please post the portrait activity_main layout.
    – Mike M.
    Nov 23 at 0:10










  • Please post your activity_main especially if you have custom activity_main for portrait, and post your manifest
    – Momen Zaqout
    Nov 23 at 0:30










  • Found the error and edited my post with the correct code. Thanks to you two!
    – Fleet23
    Nov 23 at 18:06














-1












-1








-1







I'm having trouble adding a fragment to an activity.



When i open the app in Landscape mode the fragment is working as it should and displays the infos next to the list.
But when i try to display infos in portrait mode the app starts a new activity but doesnt display the fragment.



The activity that is opened with the fragment is empty.
The App doesn't even go into the function onCreateView because the Log.d(TAG, "onCreateView: ciao") is never executed.



Here is the Java code:



public class ActivityMain extends AppCompatActivity {
private static final String TAG = "ActivityMain";



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: started");

LeftFragment leftFragment = new LeftFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.mainLayout, leftFragment).commit();
}


}



public class LeftFragment extends Fragment {
private static final String TAG = "LeftFragment";

public LeftFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView: ciao");
View view = inflater.inflate(R.layout.frag, container, false);

ListView listView = (ListView) view.findViewById(R.id.list2);

/* 100 Zufallszahlen in ArrayList "names" schreiben */
Random rand = new Random();
int n = rand.nextInt(200)+1;
for(int i = 0; i <= 100; i++)
{
n = rand.nextInt(200)+1;
DataContainer.names.add(Integer.toString(n));
}

/* Arrayadapter initialisieren, der Adapter holt sich seine Daten aus der ihm zugewiesenen Liste "names"
die listview bekommt einen Adapter zugewiesen, der sie mit den Daten aus der Liste versorgt
*/
ArrayAdapter adapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, DataContainer.names);
listView.setAdapter(adapter);
/* Onclick Listener für die Listenelemente */
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedData = DataContainer.names.get(position);
DetailsFragment detailsFragment = new DetailsFragment();

Bundle bundle = new Bundle();
bundle.putString("selected_data", selectedData);

detailsFragment.setArguments(bundle);

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
Intent intent;
intent = new Intent(getActivity(), Activity_second.class);
Log.w(TAG, "onItemClick: " + intent);
intent.putExtra("selected_data", selectedData);
startActivity(intent);
}
else
{
FragmentManager manager = getFragmentManager();
manager.beginTransaction().replace(R.id.detailLayout, detailsFragment).commit();
}
}
});
return view;
}


public static class DataContainer{
static ArrayList<String> names = new ArrayList<>();
//static ArrayList<String> details = new ArrayList<>();
}


}



    public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_details, container, false);
TextView TextDetailHeadline = (TextView)view.findViewById(R.id.TextDetailHeadline);
TextView TextDetails = (TextView)view.findViewById(R.id.TextDetails);
Bundle bundle = getArguments();

if(bundle != null) {
if (bundle.getString("selected_data") != null){
TextDetailHeadline.setText(bundle.getString("selected_data"));
TextDetails.setText(bundle.getString("selected_data"));
}
}
return view;
}


}



public class Activity_second extends AppCompatActivity {

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

FragmentManager manager = getSupportFragmentManager();
DetailsFragment detailsFragment = new DetailsFragment();
detailsFragment.setArguments(getIntent().getExtras());
manager.beginTransaction().replace(R.id.detailLayout, detailsFragment).commit();
}


}



Found the error by myself: my Activity_Second linked to the wrong XML file in line:



    setContentView(R.layout.activity_details);


..Yikes










share|improve this question















I'm having trouble adding a fragment to an activity.



When i open the app in Landscape mode the fragment is working as it should and displays the infos next to the list.
But when i try to display infos in portrait mode the app starts a new activity but doesnt display the fragment.



The activity that is opened with the fragment is empty.
The App doesn't even go into the function onCreateView because the Log.d(TAG, "onCreateView: ciao") is never executed.



Here is the Java code:



public class ActivityMain extends AppCompatActivity {
private static final String TAG = "ActivityMain";



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: started");

LeftFragment leftFragment = new LeftFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.mainLayout, leftFragment).commit();
}


}



public class LeftFragment extends Fragment {
private static final String TAG = "LeftFragment";

public LeftFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView: ciao");
View view = inflater.inflate(R.layout.frag, container, false);

ListView listView = (ListView) view.findViewById(R.id.list2);

/* 100 Zufallszahlen in ArrayList "names" schreiben */
Random rand = new Random();
int n = rand.nextInt(200)+1;
for(int i = 0; i <= 100; i++)
{
n = rand.nextInt(200)+1;
DataContainer.names.add(Integer.toString(n));
}

/* Arrayadapter initialisieren, der Adapter holt sich seine Daten aus der ihm zugewiesenen Liste "names"
die listview bekommt einen Adapter zugewiesen, der sie mit den Daten aus der Liste versorgt
*/
ArrayAdapter adapter = new ArrayAdapter(getContext(), android.R.layout.simple_list_item_1, DataContainer.names);
listView.setAdapter(adapter);
/* Onclick Listener für die Listenelemente */
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedData = DataContainer.names.get(position);
DetailsFragment detailsFragment = new DetailsFragment();

Bundle bundle = new Bundle();
bundle.putString("selected_data", selectedData);

detailsFragment.setArguments(bundle);

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
Intent intent;
intent = new Intent(getActivity(), Activity_second.class);
Log.w(TAG, "onItemClick: " + intent);
intent.putExtra("selected_data", selectedData);
startActivity(intent);
}
else
{
FragmentManager manager = getFragmentManager();
manager.beginTransaction().replace(R.id.detailLayout, detailsFragment).commit();
}
}
});
return view;
}


public static class DataContainer{
static ArrayList<String> names = new ArrayList<>();
//static ArrayList<String> details = new ArrayList<>();
}


}



    public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_details, container, false);
TextView TextDetailHeadline = (TextView)view.findViewById(R.id.TextDetailHeadline);
TextView TextDetails = (TextView)view.findViewById(R.id.TextDetails);
Bundle bundle = getArguments();

if(bundle != null) {
if (bundle.getString("selected_data") != null){
TextDetailHeadline.setText(bundle.getString("selected_data"));
TextDetails.setText(bundle.getString("selected_data"));
}
}
return view;
}


}



public class Activity_second extends AppCompatActivity {

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

FragmentManager manager = getSupportFragmentManager();
DetailsFragment detailsFragment = new DetailsFragment();
detailsFragment.setArguments(getIntent().getExtras());
manager.beginTransaction().replace(R.id.detailLayout, detailsFragment).commit();
}


}



Found the error by myself: my Activity_Second linked to the wrong XML file in line:



    setContentView(R.layout.activity_details);


..Yikes







android android-fragments android-intent android-activity






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 18:06

























asked Nov 22 at 23:31









Fleet23

11




11












  • Please post the portrait activity_main layout.
    – Mike M.
    Nov 23 at 0:10










  • Please post your activity_main especially if you have custom activity_main for portrait, and post your manifest
    – Momen Zaqout
    Nov 23 at 0:30










  • Found the error and edited my post with the correct code. Thanks to you two!
    – Fleet23
    Nov 23 at 18:06


















  • Please post the portrait activity_main layout.
    – Mike M.
    Nov 23 at 0:10










  • Please post your activity_main especially if you have custom activity_main for portrait, and post your manifest
    – Momen Zaqout
    Nov 23 at 0:30










  • Found the error and edited my post with the correct code. Thanks to you two!
    – Fleet23
    Nov 23 at 18:06
















Please post the portrait activity_main layout.
– Mike M.
Nov 23 at 0:10




Please post the portrait activity_main layout.
– Mike M.
Nov 23 at 0:10












Please post your activity_main especially if you have custom activity_main for portrait, and post your manifest
– Momen Zaqout
Nov 23 at 0:30




Please post your activity_main especially if you have custom activity_main for portrait, and post your manifest
– Momen Zaqout
Nov 23 at 0:30












Found the error and edited my post with the correct code. Thanks to you two!
– Fleet23
Nov 23 at 18:06




Found the error and edited my post with the correct code. Thanks to you two!
– Fleet23
Nov 23 at 18:06

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439071%2fadd-fragment-to-an-activity%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439071%2fadd-fragment-to-an-activity%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks