i have a button called ac and when i click on it, i want to hide my two fragment called a and b, but this is not happening, what do I need to do to make this work?
package cmsc436.lab5;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class Lab5Activity extends Activity implements button2interface, button1interface{
/** Called when the activity is first created. */
button1 a;
button2 b ;
FragmentManager fragmentManager;
FragmentTransaction fragmentTransaction;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
a= new button1();
b=new button2();
final LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setId(1);
fragmentManager = getFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
//a.getActivity().findViewById(1);
fragmentTransaction.add(linearLayout.getId(), a);
fragmentTransaction.add(linearLayout.getId(), b);
fragmentTransaction.commit();
final Button ac =new Button(this);
ac.setText("c button");
linearLayout.addView(ac);
setContentView(linearLayout);
ac.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
fragmentTransaction.hide(a);
fragmentTransaction.hide(b);
}
});
}
#Override
public void buttonClick1() {
if (b.isHidden()) {
fragmentTransaction.show(b);
}
else {
fragmentTransaction.hide(b);
}
}
#Override
public void buttonClick2() {
if (a.isHidden()) {
fragmentTransaction.show(a);
}
else {
fragmentTransaction.hide(a);
}
}
}
I haven't really worked with fragments before, but it looks like you're trying to re-use a FragmentTransaction that's already been committed. You don't want a member variable for your FragmentTransaction; you should create a new one every time you need it:
public void onClick(View v) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.hide(a);
fragmentTransaction.hide(b);
fragmentTransaction.commit();
}
Related
I have a Fragment pager adapter with five fragments and inside the third fragment I want to call an other activity which sends a selected picture to the container. It works fine but, every time the picture is selected from the other activity and the pager adapter starts from the first fragment in the fragment-activity instead of third fragment-activity. Do I have to force the pager adapter from the other activity or should I change something in the fragment-activity?
Code:
Fragment-activity:
package com.example.android.womb_the_game;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabLayout;
public class Circulation extends FragmentActivity {
ViewPager vp;
public static FragmentPagerAdapter adapterViewPager;
#Override
protected void onCreate(Bundle onSavedInstanceState) {
super.onCreate(onSavedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_circulation);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
vp = findViewById(R.id.view_pager);
adapterViewPager=new Adapter_Circulation(getSupportFragmentManager(), this);
vp.setAdapter(adapterViewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(vp);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}
FragmentPagerAdapter:
package com.example.android.womb_the_game;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import android.content.Context;
public class Adapter_Circulation extends FragmentPagerAdapter {
private static int NUM_ITEMS = 5;
Context context;
public Adapter_Circulation(FragmentManager fm, Context c) {
super(fm);
this.context = c;
}
#Override
public Fragment getItem(int position) {
switch (position)
{
case 0:
return Frg0.newInstance ();
case 1:
return Frg1.newInstance();
case 2:
return Frg2.newInstance();
case 3:
return Frg3.newInstance();
case 4:
return Frg4.newInstance();
}
return null; //does not happen
}
#Override
public int getCount() {
return NUM_ITEMS; //three fragments
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "RULES";
case 1:
return "EVENTS";
case 2:
return "PLAN";
case 3:
return "SHOOT";
case 4:
return "JUMP";
}
return null;
}
}
Fragment3:
package com.example.android.womb_the_game;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
public class Frg3 extends Fragment{
private ImageButton shoot;
private ImageButton snipe;
private ImageButton aim;
public static Frg3 newInstance() {
Bundle args = new Bundle();
Frg3 fragment = new Frg3();
fragment.setArguments(args);
return fragment;
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable final Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_frg3, container, false);
shoot = (ImageButton) rootView.findViewById(R.id.shoot);
snipe = (ImageButton) rootView.findViewById(R.id.snipe);
aim = (ImageButton) rootView.findViewById(R.id.aim);
final View.OnClickListener mListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.shoot:
FragmentManager FM0 = getFragmentManager();
FragmentTransaction FT0 = FM0.beginTransaction();
frg_shoot F10 = new frg_shoot();
FT0.add(R.id.fragment_container1, F10);
FT0.replace(R.id.fragment_container1, F10);
FT0.commit();
break;
case R.id.snipe:
FragmentManager FM = getFragmentManager();
FragmentTransaction FT = FM.beginTransaction();
frg_snipe F1 = new frg_snipe();
FT.add(R.id.fragment_container1, F1);
FT.replace(R.id.fragment_container1, F1);
FT.commit();
break;
case R.id.aim:
FragmentManager FM1 = getFragmentManager();
FragmentTransaction FT1 = FM1.beginTransaction();
frg_aim F11 = new frg_aim();
FT1.add(R.id.fragment_container1, F11);
FT1.replace(R.id.fragment_container1, F11);
FT1.commit();
break;
}
}
};
rootView.findViewById(R.id.shoot).setOnClickListener(mListener);
rootView.findViewById(R.id.snipe).setOnClickListener(mListener);
rootView.findViewById(R.id.aim).setOnClickListener(mListener);
return rootView;
}
}
Other Activity:
package com.example.android.womb_the_game;
import androidx.fragment.app.FragmentActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
public class choose_dice_column extends FragmentActivity {
private ImageButton b1;
private ImageView im1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_choose_dice_column);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
b1 = (ImageButton) findViewById(R.id.b1);
im1 = findViewById(R.id.im1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(choose_dice_column.this, Circulation.class);
i.putExtra("resid1",R.drawable.n1);
startActivity(i);
}
});
}
}
The problem is, whenever you get back to your activity, you are creating a new adapter. So add the following to your Circulation-Activity to keep the state of the adapter:
if(adapterViewPager == null)
{
adapterViewPager = new Adapter_Circulation(getSupportFragmentManager(), this);
}
I have a navbar which is supposed to be hidden when a button is pressed, but nothing happens.
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView mavisTxt;
private Button testBtn;
private Fragment navigationBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initVariables();
}
private void initVariables() {
navigationBar = new HeaderNav();
mavisTxt = (TextView)findViewById(R.id.mavisTxt);
testBtn = (Button)findViewById(R.id.testBtn);
testBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.hide(navigationBar)
.commit();
mavisTxt.setText("navbar is hidden");
}
});
}
}
Here is the java file for the fragment
public class HeaderNav extends Fragment {
private static Button homeBtn, optionsBtn, connectionBtn, micBtn, aboutBtn;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,#Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.navbar, container, false);
homeBtn = (Button)view.findViewById(R.id.homeBtn);
optionsBtn = (Button)view.findViewById(R.id.optionsBtn);
connectionBtn = (Button)view.findViewById(R.id.micBtn);
micBtn = (Button)view.findViewById(R.id.homeBtn);
aboutBtn = (Button)view.findViewById(R.id.aboutBtn);
return view;
}
}
and the fragment tag in the main_activity xml :
<fragment
android:id="#+id/navigationBar"
android:name="com.example.egi.mavisme.HeaderNav"
android:layout_width="fill_parent"
android:layout_height="60dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout="#layout/navbar" />
what am I doing wrong? none of the solutions here in SO works for me. Someone help me.
EDIT:
I have edited my code and now it hides. I have another issue when adding a custom animation, I am getting a
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.ViewGroup.startViewTransition(android.view.View)' on a null object reference
Here is my code now :
public class MainActivity extends AppCompatActivity {
private TextView mavisTxt;
private Button testBtn;
private HeaderNav navigationBar;
private FragmentManager fm;
private FragmentTransaction ft;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initVariables();
}
private void initVariables() {
fm = getSupportFragmentManager();
ft = fm.beginTransaction();
navigationBar = (HeaderNav)fm.findFragmentById(R.id.navigationBar);
mavisTxt = (TextView)findViewById(R.id.mavisTxt);
testBtn = (Button)findViewById(R.id.testBtn);
testBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
ft.hide(navigationBar)
.commit();
if(navigationBar.isHidden()) {mavisTxt.setText("navbar is hidden");}
}
});
}
}
Here are some things to note-
Use the ActionBar/ToolBar, this isn't iOS
You have to add a fragment first
You are using the show call instead of hiding it
testBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.**show**(navigationBar)
.commit();
mavisTxt.setText("navbar is hidden");
}
});
Im developing an app that displays a map on a fragment. Everything is set up, but there's small thing that is not letting me finish it.
gMapFragment.java:
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class gMapFragment extends Fragment implements OnMapReadyCallback {
public gMapFragment() {
// Required empty public constructor
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_map, container, false);
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MapFragment fragment = (MapFragment) getChildFragmentManager().findFragmentById(R.id.map);
fragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
LatLng marker = new LatLng(46.33328, 15.38173);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker,13));
googleMap.addMarker(new MarkerOptions().title(("HERE WE ARE!")).position(marker));
}
}
I call this fragment from button in another frament and this is the method that calls it:
public void onClick(View view) {
Fragment fragment = null;
switch (view.getId()) {
case R.id.SearchButton:
fragment = new gMapFragment();
replaceFragment(fragment);
break;
}
}
public void replaceFragment(Fragment someFragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, someFragment);
transaction.addToBackStack(null);
transaction.commit();
}
I get error "gMapFragment cannot be converted to fragment" Any idea how to fix this? thanks!
First, make sure that you're consistent with your Fragment imports. Use either import android.app.Fragment in all classes or import android.support.v4.app.Fragment; in all classes. Don't mix-and-match.
Then, add this to the Activity:
public void openMapFragment() {
Fragment fragment = new gMapFragment();
replaceFragment(fragment);
}
//moved to Activity from the Fragment
public void replaceFragment(Fragment someFragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, someFragment);
transaction.addToBackStack(null);
transaction.commit();
}
Then, in the Fragment onClick() method, just call into the Activity to do the FragmentTransaction:
public void onClick(View view) {
switch (view.getId()) {
case R.id.SearchButton:
MainActivity activity = (MainActivity) getActivity();
if (activity != null) {
activity.openMapFragment();
}
break;
}
}
I'm working on an a Quiz App which has five fragments and every fragment has one Question in it. Fragments move in two ways.
When user clicks on an option then the next fragment appears Immediately.
I've a countdown timer that if user does not click on any option then after 10 seconds the fragment is moved to new fragment.
I've implemented the click functions. when user clicks on the option app takes it to the next fragment but if in that next fragment user clicks no option then app stops Immediately.
Kindly have a look on this, I'm sharing code.
Question.java (Fragment1)
package com.example.sheikhspc.testapp;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class Question1 extends Fragment implements View.OnClickListener {
public Question1() {
// Required empty public constructor
}
int counter = 0;
View view;
Fragment frag = null;
TextView tv;
ImageView imageView1, imageViewR, imageView2;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_question1, container, false);
imageView1 = (ImageView) view.findViewById(R.id.wrong1);
imageViewR = (ImageView) view.findViewById(R.id.right);
imageView2 = (ImageView) view.findViewById(R.id.wrong2);
tv = (TextView) view.findViewById(R.id.timer1);
imageView1.setOnClickListener(this);
imageViewR.setOnClickListener(this);
imageView2.setOnClickListener(this);
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
tv.setText("Remaining Time: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
frag = new Question2();
FragmentManager fmm = getFragmentManager();
FragmentTransaction fragmentTransactionn = fmm.beginTransaction();
fragmentTransactionn.addToBackStack(null);
fragmentTransactionn.replace(R.id.container, frag);
fragmentTransactionn.commit();
}
}.start();
return view;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.wrong1:
frag = new Question2();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.container,frag);
fragmentTransaction.commit();
break;
case R.id.right:
frag = new Question2();
FragmentManager fm1 = getFragmentManager();
FragmentTransaction fragmentTransaction1 = fm1.beginTransaction();
fragmentTransaction1.replace(R.id.container,frag);
fragmentTransaction1.commit();
break;
case R.id.wrong2:
frag = new Question2();
FragmentManager fm2 = getFragmentManager();
FragmentTransaction fragmentTransaction2 = fm2.beginTransaction();
fragmentTransaction2.replace(R.id.container,frag);
fragmentTransaction2.commit();
break;
default:
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
tv.setText("Remaining Time: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
frag = new Question2();
FragmentManager fmm = getFragmentManager();
FragmentTransaction fragmentTransactionn = fmm.beginTransaction();
fragmentTransactionn.addToBackStack(null);
fragmentTransactionn.replace(R.id.container, frag);
fragmentTransactionn.commit();
}
}.start();
break;
}
}
}
Question2.java (Fragment2)
package com.example.sheikhspc.testapp;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.app.Fragment;
import android.os.CountDownTimer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class Question2 extends Fragment implements View.OnClickListener {
public Question2() {
// Required empty public constructor
}
Fragment frag = null;
TextView tv;
View view;
ImageView imageView1, imageViewR, imageView2;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_question2, container, false);
tv = (TextView)view.findViewById(R.id.timer2);
imageView1 = (ImageView) view.findViewById(R.id.wrong1);
imageViewR = (ImageView) view.findViewById(R.id.right);
imageView2 = (ImageView) view.findViewById(R.id.wrong2);
imageView1.setOnClickListener(this);
imageViewR.setOnClickListener(this);
imageView2.setOnClickListener(this);
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
tv.setText("seconds remaining: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
tv.setText("done!");
frag = new Question3();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.container,frag);
fragmentTransaction.commit();
}
}.start();
return view;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.wrong1:
frag = new Question3();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.container,frag);
fragmentTransaction.commit();
break;
case R.id.right:
frag = new Question3();
FragmentManager fm1 = getFragmentManager();
FragmentTransaction fragmentTransaction1 = fm1.beginTransaction();
fragmentTransaction1.replace(R.id.container,frag);
fragmentTransaction1.commit();
break;
case R.id.wrong2:
frag = new Question3();
FragmentManager fm2 = getFragmentManager();
FragmentTransaction fragmentTransaction2 = fm2.beginTransaction();
fragmentTransaction2.replace(R.id.container,frag);
fragmentTransaction2.commit();
break;
default:
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
tv.setText("seconds remaining: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
tv.setText("done!");
frag = new Question3();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.container,frag);
fragmentTransaction.commit();
}
}.start();
break;
}
}
}
Error Log
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sheikhspc.testapp, PID: 32330
java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.FragmentTransaction android.app.FragmentManager.beginTransaction()' on a null object reference
at com.example.sheikhspc.testapp.Question1$1.onFinish(Question1.java:58)
at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:127)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
I/Process: Sending signal. PID: 32330 SIG: 9
Application terminated.
Second.java (Activity where all the fragments are displayed)
package com.example.sheikhspc.testapp;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.app.Fragment;
import android.os.CountDownTimer;
import android.provider.DocumentsContract;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class Second extends AppCompatActivity {
Fragment frag;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
frag = new Question1();
fragmentTransaction.replace(R.id.container,frag);
fragmentTransaction.commit();
}
}
You will need to cancel the timer before you replace the fragment on click.
When you create a new instance of CountDownTimer in onCreate method assign it to a variable for e.g.
CountDownTimer timer = new CountDownTimer(10000, 1000) {...};
timer.start();
And then in onClick method i.e. when user clicks any option stop the timer for e.g.
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnNext:
// Add this line to avoid crash
timer.cancel();
frag = new FragmentQuestion2();
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.container,frag);
fragmentTransaction.commit();
break;
}
MainActivity
package example.antonio.activitydemo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.support.v4.app.*;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final FragmentManager fragmentManager = getSupportFragmentManager();
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirstFragment fragment = new FirstFragment();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container, fragment);
fragmentTransaction.commit();
}
});
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SecondFragment fragment = new SecondFragment();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.commit();
}
});
}
}
FirstFragment
package example.antonio.activitydemo;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.View;
public class FirstFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInsanceState) {
return inflater.inflate(R.layout.first_fragment, container, false);
}
}
SecondFragment
package example.antonio.activitydemo;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.View;
public class SecondFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInsanceState) {
return inflater.inflate(R.layout.second_fragment, container, false);
}
}
Their XML files:
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start1"
android:id="#+id/button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start2"
android:id="#+id/button2"/>
</LinearLayout>
first_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First!"
android:textColor="#ffffff"/>
</FrameLayout>
second_fragment.xml just like the previous one, only with android:text="Second!"
Here is the strange thing, when I click for 4 times the Start1 button then I get 4 times the word First!, until here all normal. Following if I click Start2 button what I would expect is the erase of the 4 previous fragments and the add of just a new one, with appearance of Second!. This is what I understood from the docs:
Replace an existing fragment that was added to a container. This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.
But here is what I get:
I tried to read the source code, maybe I found where is the "problem", I would like to know what you think and if it's me that I'm making a mistake somewhere.
Here is the snippet from BackStackRecord.java, run() method:
case OP_REPLACE: {
Fragment f = op.fragment;
int containerId = f.mContainerId;
if (mManager.mAdded != null) {
for (int i = 0; i < mManager.mAdded.size(); i++) {
Fragment old = mManager.mAdded.get(i);
if (FragmentManagerImpl.DEBUG) {
Log.v(TAG,
"OP_REPLACE: adding=" + f + " old=" + old);
}
if (old.mContainerId == containerId) {
if (old == f) {
op.fragment = f = null;
} else {
if (op.removed == null) {
op.removed = new ArrayList<Fragment>();
}
op.removed.add(old);
old.mNextAnim = op.exitAnim;
if (mAddToBackStack) {
old.mBackStackNesting += 1;
if (FragmentManagerImpl.DEBUG) {
Log.v(TAG, "Bump nesting of "
+ old + " to " + old.mBackStackNesting);
}
}
mManager.removeFragment(old, mTransition, mTransitionStyle);
}
}
}
}
and here is the snippet from FragmentManager.java:
public void removeFragment(Fragment fragment, int transition, int transitionStyle) {
if (DEBUG) Log.v(TAG, "remove: " + fragment + " nesting=" + fragment.mBackStackNesting);
final boolean inactive = !fragment.isInBackStack();
if (!fragment.mDetached || inactive) {
if (false) {
// Would be nice to catch a bad remove here, but we need
// time to test this to make sure we aren't crashes cases
// where it is not a problem.
if (!mAdded.contains(fragment)) {
throw new IllegalStateException("Fragment not added: " + fragment);
}
}
**if (mAdded != null) {
mAdded.remove(fragment);
}**
if (fragment.mHasMenu && fragment.mMenuVisible) {
mNeedMenuInvalidate = true;
}
fragment.mAdded = false;
fragment.mRemoving = true;
moveToState(fragment, inactive ? Fragment.INITIALIZING : Fragment.CREATED,
transition, transitionStyle, false);
}
}
As you can see, the removeFragment method removes a frament from mAdded but after in the run() method, the i index isn't modified, it restart from where it left, in such way it can lose some elements...
you cannot achieve this scenario with the linearlayout as a container.
If you try using the fragment as a container in linearlayout you can achieve this. please refer this link for further clarifications
Android - fragment .replace() doesn't replace content - puts it on top
Otherwise use the below code for button click actions,
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FirstFragment fragment = new FirstFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.container, fragment).addToBackStack(null);
fragmentTransaction.commit();
}
});
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SecondFragment fragment = new SecondFragment();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
for(int i=0; i < getSupportFragmentManager().getBackStackEntryCount(); i++)
getSupportFragmentManager().popBackStack();
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.commit();
}
});
Hope this will help you.. Thanks