Answer the question
In order to leave comments, you need to log in
How to make the main activity, at startup, show a fragment?
Good
for the second day, I’m fighting, I can’t understand, there is an activity, there are fragments in it, there is a menu, with the help of it all the fragments switch normally, but I need it to show the first fragment at startup, but alas ..
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import www.finefood.CreateNewUser.CreateNewUser;
import www.finefood.R;
import www.finefood.TestData.TestData;
import www.finefood.main.Fragment.AbstractionFragment;
import www.finefood.main.Fragment.CreateMenuFragment;
import www.finefood.main.Fragment.SettingMenuFragment;
import www.finefood.main.Fragment.WatchProgressFragment;
public class MainActivity extends AppCompatActivity {
private String[] titles;
private ListView drawerList;
FragmentTransaction transaction;
FragmentManager manager;
AbstractionFragment createMenuFragment = new CreateMenuFragment();
AbstractionFragment watchProgressFragment = new WatchProgressFragment();
SettingMenuFragment settingMenuFragment = new SettingMenuFragment();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
titles = getResources().getStringArray(R.array.menu_fragment);
drawerList = (ListView) findViewById(R.id.drawer);
drawerList.setAdapter(new CustomListAdapter(this,
android.R.layout.simple_list_item_1, titles));
drawerList.setOnItemClickListener(new DrawerItemClickListener());
}
@Override
protected void onStart() {
super.onStart();
//Заплатака
if (!TestData.testNewUser)
//Заплатка
{
Intent intent = new Intent(getApplicationContext(), CreateNewUser.class);
startActivity(intent);
this.finish();
}
manager = getSupportFragmentManager();
transaction = manager.beginTransaction();
transaction.add(R.id.drawer_container, createMenuFragment, createMenuFragment.getTitle());
}
/*
* приватный класс, выводит необходимый фрагмент в MainActivity по нажатию меню.
* Возможно стоит вынести отдельно, но в конструктор нужно передать ссылку на FragmentActivity
*/
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
manager = getSupportFragmentManager();
transaction = manager.beginTransaction();
switch (position) {
case 0:
if (manager.findFragmentByTag("CreateMenuFragment") == null)
transaction.replace(R.id.drawer_container, createMenuFragment, createMenuFragment.getTitle());
break;
case 1:
if (manager.findFragmentByTag("WatchProgressFragment") == null)
transaction.replace(R.id.drawer_container, watchProgressFragment, watchProgressFragment.getTitle());
break;
case 2:
if (manager.findFragmentByTag("SettingMenuFragment") == null)
transaction.replace(R.id.drawer_container, settingMenuFragment, settingMenuFragment.getTitle());
break;
}
transaction.commit();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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/colorStage"
android:id="@+id/drawer_layout">
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.widget.DrawerLayout>
<ListView
android:id="@+id/drawer"
android:dividerHeight="5dp"
android:background="@color/listViewBackground"
android:choiceMode="singleChoice"
android:layout_gravity="start"
android:layout_width="300dp"
android:layout_height="match_parent">
</ListView>
</android.support.v4.widget.DrawerLayout>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question