Please tell me how to change the fragment so that there are skip buttons (when clicked, we will simply enter the application) and hide (when clicked, the Fragment will no longer be shown). I added these buttons to the fragment xml file and added listeners for the hide and skip buttons, but how to proceed further?

MainActivity code:

package ru.by_em.myfragment; import android.app.FragmentManager; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends ActionBarActivity { FragmentManager fragmentManager; PreferenceHelper preferenceHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); PreferenceHelper.getInstance().init(getApplicationContext()); preferenceHelper = PreferenceHelper.getInstance(); fragmentManager = getFragmentManager(); runSplash(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); MenuItem splashItem = menu.findItem(R.id.action_splash); splashItem.setChecked(preferenceHelper.getBoolean(PreferenceHelper.SPLASH_IS_INVISIBLE)); 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. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_splash) { item.setChecked(!item.isChecked()); preferenceHelper.putBoolean(PreferenceHelper.SPLASH_IS_INVISIBLE, item.isChecked()); return true; } return super.onOptionsItemSelected(item); } private void runSplash() { if(!preferenceHelper.getBoolean(PreferenceHelper.SPLASH_IS_INVISIBLE)) { BlankFragment splashFragment = new BlankFragment(); fragmentManager.beginTransaction() .replace(R.id.first, splashFragment) .addToBackStack(null) .commit(); } } 

}

code activity_main:

  <?xml version="1.0" encoding="utf-8"?> 

 <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:id="@+id/textView" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_below="@+id/textView" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> 

BlankFragment code:

  package ru.by_em.myfragment; import android.app.Fragment; import android.app.FragmentManager; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import java.util.concurrent.TimeUnit; /** * A simple {@link Fragment} subclass. */ public class BlankFragment extends Fragment { FragmentManager fragmentManager; public BlankFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment SplashTask splashTask = new SplashTask(); splashTask.execute(); final String LOG_TAG = "myLogs"; View v = inflater.inflate(R.layout.fragment_blank, null); Button button = (Button) v.findViewById(R.id.button_Skip); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Log.d(LOG_TAG, "Button_Skip"); // Create new fragment and transaction } }); Button button2 = (Button) v.findViewById(R.id.button2); button2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Log.d(LOG_TAG, "Button_OK"); } }); return v; } class SplashTask extends AsyncTask<Void, Void, Void> { @Override protected Void doInBackground(Void... params) { try { TimeUnit.SECONDS.sleep(40); } catch (InterruptedException e) { e.printStackTrace(); } if(getActivity() != null) { getActivity().getFragmentManager().popBackStack(); } return null; } } } 

fragment_blank code:

 <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/splashreminder"/> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Skip" android:id="@+id/button_Skip" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="OK" android:id="@+id/button2" android:layout_gravity="right" /> </LinearLayout> 

PreferenceHelper code:

  package ru.by_em.myfragment; import android.content.Context; import android.content.SharedPreferences; /** * Created by Admin on 29.11.2015. */ public class PreferenceHelper { public static final String SPLASH_IS_INVISIBLE = "splash_is_invisible"; private static PreferenceHelper instance; private Context context; private SharedPreferences preferences; private PreferenceHelper() { } public static PreferenceHelper getInstance() { if (instance ==null) { instance = new PreferenceHelper(); } return instance; } public void init(Context context) { this.context = context; preferences = context.getSharedPreferences("preferences", Context.MODE_PRIVATE); } public void putBoolean(String key, boolean value) { SharedPreferences.Editor editor = preferences.edit(); editor.putBoolean(key, value); editor.apply(); } public boolean getBoolean(String key) { return preferences.getBoolean(key, false); } } 

    0