0

i try to change the view of my App using a Fragment. I tried on this several days, so i made a clean code to present my problem. I have an MainActivity with a OptionsMenu, in this OptionsMenu i have an item called "action_connect". The first view of my App should display "welcome to MainActivity!". When someone clicks the "action_connect" item, I want the View to change to my Fragment in which "welcome to Fragment!" should be displayed. Problem: The Fragment overlays the Activity Text instead of switching to the new view.

I hope you can help me to solve this. Thanks in advance.

Heres my code:

MainActivity:

public class MainActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstance){
    super.onCreate(savedInstance);
    setContentView(R.layout.main_activity);



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    MainActivityFragment mMainActivityFragment = new MainActivityFragment();

    int id = item.getItemId();

    if (id == R.id.action_connect) {

        ft.replace(R.id.default_container, mMainActivityFragment);
    }


    ft.commit();
    return super.onOptionsItemSelected(item);

}

Fragment:

public class MainActivityFragment extends Fragment{

@Override
public void onCreate(Bundle savedInstance){
    super.onCreate(savedInstance);



}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){


    View rootView = inflater.inflate(R.layout.fragment_connect, parent, false);

    return rootView;
}

}

Fragment-XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:text="Welcome in Fragment!"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="36dp" />

Activity-XML:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/default_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></FrameLayout>

    <TextView
        android:text="Welcome in MainActivity!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="36dp" />


</RelativeLayout>
keko
  • 37
  • 10

2 Answers2

1

As Cedric stated it is correct, To not see overlaying elements / fragments / activity just add a background color to your xml on your fragment:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent" android:background="@color/white">

Just add a finish() at end of your code and remove the flags, this will destroy the ViewActivity and it will be removed from your stack!

see https://stackoverflow.com/a/38376737/4230939

Community
  • 1
  • 1
Jono M
  • 91
  • 2
  • 9
  • what flags do you mean and how do i implement the "finish();" and where exactly? – keko Feb 27 '17 at 14:41
  • Sorry I over looked your question. For your application you don't need to worry about removing the flags or adding the finish(). Only using the background color. – Jono M Feb 27 '17 at 15:02
  • Yeah, i thought so and did the background thing! :-) – keko Feb 27 '17 at 15:17
0

Ok so you are doing nothing wrong. What happens here is the normal behaviour of a fragment, and this is how it should be used.

Fragments are part of Activities, they are like normals Views except they have lifecycle like Activities. You cannot create a Fragment without an Activity, it has to be attach to an activity , a lifecycle method is even called onAttach() wich refers to the moment the fragment is attach to an activity.

I strongly recomend you to read more about fragments here :

https://developer.android.com/guide/components/fragments.html

What you may want is just to switch to a second Activity in order to display new content and erase previous instead on being in overlay on it.

Cedric Franck
  • 526
  • 4
  • 9
  • I want to switch the view by the fragment. Long term goal is to switch between different fragments in ONE activity. I just dont know how to do so as i have lack of experience. – keko Feb 27 '17 at 14:29
  • you replaced the view (not activity) default_container by your fragment here – Cedric Franck Feb 27 '17 at 14:30
  • And how could i replace the activitys view using my fragment? so it wont get overlayed? Or is my approach just wrong? – keko Feb 27 '17 at 14:34
  • You can use R.id.content instead of R.id.default_container – Cedric Franck Feb 27 '17 at 14:36
  • How do i have to implement "R.id.content", do i have to declare something? as "content" just turns red --> GOT IT, changed it to: "android.R.id.content" – keko Feb 27 '17 at 14:42
  • Sorry its android.R.id.content and no, its just ft.replace(android.R.id.content, mMainActivityFragment); – Cedric Franck Feb 27 '17 at 14:45