My chronometer stops when i change the activity. How can i prevent this?











up vote
0
down vote

favorite












I have one activity and three fragments. In fragment(1), i have a chronometer that works perfectly. Firstly, i click chronometer start button. When chronometer shows 00:10, i open fragment(2). Then i return after 6 seconds, chronometer shows 00:00. I want it to show 00:16 and keep working while i changing fragments.



Fragment (1) xml.



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<Chronometer
android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:textSize="70dp"
android:textColor="#fff"
/>

<Button
android:id="@+id/btn_Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_below="@+id/chronometer"
android:background="@drawable/btn_bg"
android:textColor="#fff"
/>

<Button
android:id="@+id/btn_Pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:layout_centerHorizontal="true"
android:layout_below="@+id/btn_Start"
android:background="@drawable/btn_bg"
android:layout_marginTop="20dp"
android:textColor="#fff"/>

<Button
android:id="@+id/btn_Reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:layout_centerHorizontal="true"
android:layout_below="@+id/btn_Pause"
android:background="@drawable/btn_bg"
android:layout_marginTop="20dp"
android:textColor="#fff"/>

<Button
android:id="@+id/btn_Back"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btn_Reset"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="0dp"
android:background="@drawable/btn_bg_2"
android:drawableRight="@drawable/ic_fast_rewind_black_24dp"
android:textColor="#fff" />

<Button
android:id="@+id/btn_Forward"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btn_Reset"
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
android:background="@drawable/btn_bg_2"
android:drawableLeft="@drawable/ic_fast_forward_black_24dp"
android:textColor="#fff" />

</RelativeLayout>


This is my fragment(1) java code.



public class HomeFragment extends Fragment {


private Button btn_start;
private Button btn_pause;
private Button btn_reset;
private Button btn_back;
private Button btn_forward;


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable
ViewGroup container, @Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_home, container, false);



chronometer = view.findViewById(R.id.chronometer);
chronometer.setFormat("%s");

btn_forward= view.findViewById(R.id.btn_Forward);
btn_back = view.findViewById(R.id.btn_Back);
btn_start= view.findViewById(R.id.btn_Start);
btn_pause= view.findViewById(R.id.btn_Pause);
btn_reset= view.findViewById(R.id.btn_Reset);

btn_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
backChronometer();
}
});

btn_forward.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
forwardChronometer();
}
});

btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startChronometer();
}
});

btn_pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pauseChronometer();
}
});

btn_reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pauseChronometer();
resetChronometer();
}
});

return view;
}
}


This is chronometer java code. (methods)



public class Chronometer {

public static long pauseOffset;
public static boolean running;

public static void startChronometer (){
if(!running){
chronometer.setBase(SystemClock.elapsedRealtime() - pauseOffset);
chronometer.start();
running = true;
}

}
public static void pauseChronometer (){

if (running){
pauseOffset = SystemClock.elapsedRealtime() - chronometer.getBase();
chronometer.stop();
running = false;
}

}
public static void resetChronometer (){

chronometer.setBase(SystemClock.elapsedRealtime());
pauseOffset = 0;
}
public static void forwardChronometer (){
if (running) {
chronometer.setBase(chronometer.getBase()-1000);
}
}
public static void backChronometer (){
if (running) {
chronometer.setBase(chronometer.getBase()+1000);
}
}
}


MainActivity java code.



public class MainActivity extends AppCompatActivity {

public static Chronometer chronometer;
public static TextView tv_rune;
public static SeekBar sb_rune;
public static int minrune = 0, maxrune = 35, currentrune;

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


BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);

getSupportFragmentManager().beginTransaction().replace(R.id.main_frame,
new HomeFragment()).commit();

}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem
menuItem) {

Fragment selectedFragment = null;

switch (menuItem.getItemId()){
case R.id.navigation_home:
selectedFragment = new HomeFragment();
break;

case R.id.navigation_notifications:
selectedFragment = new NotificationsFragment();
break;

case R.id.navigation_info:
selectedFragment = new InfoFragment();
break;
}


getSupportFragmentManager().beginTransaction().replace(R.id.main_frame,
selectedFragment).commit();

return true;
}
};

}


Also I want to keep seekbar datas from fragment(2) to use them in fragment (1).
Actually, I wonder how can we keep data protected.










share|improve this question
























  • Can you post also the activity code?
    – Skemelio
    Nov 7 at 21:03










  • Editted. Chronometer defined in mainactivity.
    – H. Tunc
    Nov 7 at 21:35















up vote
0
down vote

favorite












I have one activity and three fragments. In fragment(1), i have a chronometer that works perfectly. Firstly, i click chronometer start button. When chronometer shows 00:10, i open fragment(2). Then i return after 6 seconds, chronometer shows 00:00. I want it to show 00:16 and keep working while i changing fragments.



Fragment (1) xml.



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<Chronometer
android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:textSize="70dp"
android:textColor="#fff"
/>

<Button
android:id="@+id/btn_Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_below="@+id/chronometer"
android:background="@drawable/btn_bg"
android:textColor="#fff"
/>

<Button
android:id="@+id/btn_Pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:layout_centerHorizontal="true"
android:layout_below="@+id/btn_Start"
android:background="@drawable/btn_bg"
android:layout_marginTop="20dp"
android:textColor="#fff"/>

<Button
android:id="@+id/btn_Reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:layout_centerHorizontal="true"
android:layout_below="@+id/btn_Pause"
android:background="@drawable/btn_bg"
android:layout_marginTop="20dp"
android:textColor="#fff"/>

<Button
android:id="@+id/btn_Back"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btn_Reset"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="0dp"
android:background="@drawable/btn_bg_2"
android:drawableRight="@drawable/ic_fast_rewind_black_24dp"
android:textColor="#fff" />

<Button
android:id="@+id/btn_Forward"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btn_Reset"
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
android:background="@drawable/btn_bg_2"
android:drawableLeft="@drawable/ic_fast_forward_black_24dp"
android:textColor="#fff" />

</RelativeLayout>


This is my fragment(1) java code.



public class HomeFragment extends Fragment {


private Button btn_start;
private Button btn_pause;
private Button btn_reset;
private Button btn_back;
private Button btn_forward;


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable
ViewGroup container, @Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_home, container, false);



chronometer = view.findViewById(R.id.chronometer);
chronometer.setFormat("%s");

btn_forward= view.findViewById(R.id.btn_Forward);
btn_back = view.findViewById(R.id.btn_Back);
btn_start= view.findViewById(R.id.btn_Start);
btn_pause= view.findViewById(R.id.btn_Pause);
btn_reset= view.findViewById(R.id.btn_Reset);

btn_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
backChronometer();
}
});

btn_forward.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
forwardChronometer();
}
});

btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startChronometer();
}
});

btn_pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pauseChronometer();
}
});

btn_reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pauseChronometer();
resetChronometer();
}
});

return view;
}
}


This is chronometer java code. (methods)



public class Chronometer {

public static long pauseOffset;
public static boolean running;

public static void startChronometer (){
if(!running){
chronometer.setBase(SystemClock.elapsedRealtime() - pauseOffset);
chronometer.start();
running = true;
}

}
public static void pauseChronometer (){

if (running){
pauseOffset = SystemClock.elapsedRealtime() - chronometer.getBase();
chronometer.stop();
running = false;
}

}
public static void resetChronometer (){

chronometer.setBase(SystemClock.elapsedRealtime());
pauseOffset = 0;
}
public static void forwardChronometer (){
if (running) {
chronometer.setBase(chronometer.getBase()-1000);
}
}
public static void backChronometer (){
if (running) {
chronometer.setBase(chronometer.getBase()+1000);
}
}
}


MainActivity java code.



public class MainActivity extends AppCompatActivity {

public static Chronometer chronometer;
public static TextView tv_rune;
public static SeekBar sb_rune;
public static int minrune = 0, maxrune = 35, currentrune;

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


BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);

getSupportFragmentManager().beginTransaction().replace(R.id.main_frame,
new HomeFragment()).commit();

}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem
menuItem) {

Fragment selectedFragment = null;

switch (menuItem.getItemId()){
case R.id.navigation_home:
selectedFragment = new HomeFragment();
break;

case R.id.navigation_notifications:
selectedFragment = new NotificationsFragment();
break;

case R.id.navigation_info:
selectedFragment = new InfoFragment();
break;
}


getSupportFragmentManager().beginTransaction().replace(R.id.main_frame,
selectedFragment).commit();

return true;
}
};

}


Also I want to keep seekbar datas from fragment(2) to use them in fragment (1).
Actually, I wonder how can we keep data protected.










share|improve this question
























  • Can you post also the activity code?
    – Skemelio
    Nov 7 at 21:03










  • Editted. Chronometer defined in mainactivity.
    – H. Tunc
    Nov 7 at 21:35













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have one activity and three fragments. In fragment(1), i have a chronometer that works perfectly. Firstly, i click chronometer start button. When chronometer shows 00:10, i open fragment(2). Then i return after 6 seconds, chronometer shows 00:00. I want it to show 00:16 and keep working while i changing fragments.



Fragment (1) xml.



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<Chronometer
android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:textSize="70dp"
android:textColor="#fff"
/>

<Button
android:id="@+id/btn_Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_below="@+id/chronometer"
android:background="@drawable/btn_bg"
android:textColor="#fff"
/>

<Button
android:id="@+id/btn_Pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:layout_centerHorizontal="true"
android:layout_below="@+id/btn_Start"
android:background="@drawable/btn_bg"
android:layout_marginTop="20dp"
android:textColor="#fff"/>

<Button
android:id="@+id/btn_Reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:layout_centerHorizontal="true"
android:layout_below="@+id/btn_Pause"
android:background="@drawable/btn_bg"
android:layout_marginTop="20dp"
android:textColor="#fff"/>

<Button
android:id="@+id/btn_Back"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btn_Reset"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="0dp"
android:background="@drawable/btn_bg_2"
android:drawableRight="@drawable/ic_fast_rewind_black_24dp"
android:textColor="#fff" />

<Button
android:id="@+id/btn_Forward"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btn_Reset"
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
android:background="@drawable/btn_bg_2"
android:drawableLeft="@drawable/ic_fast_forward_black_24dp"
android:textColor="#fff" />

</RelativeLayout>


This is my fragment(1) java code.



public class HomeFragment extends Fragment {


private Button btn_start;
private Button btn_pause;
private Button btn_reset;
private Button btn_back;
private Button btn_forward;


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable
ViewGroup container, @Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_home, container, false);



chronometer = view.findViewById(R.id.chronometer);
chronometer.setFormat("%s");

btn_forward= view.findViewById(R.id.btn_Forward);
btn_back = view.findViewById(R.id.btn_Back);
btn_start= view.findViewById(R.id.btn_Start);
btn_pause= view.findViewById(R.id.btn_Pause);
btn_reset= view.findViewById(R.id.btn_Reset);

btn_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
backChronometer();
}
});

btn_forward.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
forwardChronometer();
}
});

btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startChronometer();
}
});

btn_pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pauseChronometer();
}
});

btn_reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pauseChronometer();
resetChronometer();
}
});

return view;
}
}


This is chronometer java code. (methods)



public class Chronometer {

public static long pauseOffset;
public static boolean running;

public static void startChronometer (){
if(!running){
chronometer.setBase(SystemClock.elapsedRealtime() - pauseOffset);
chronometer.start();
running = true;
}

}
public static void pauseChronometer (){

if (running){
pauseOffset = SystemClock.elapsedRealtime() - chronometer.getBase();
chronometer.stop();
running = false;
}

}
public static void resetChronometer (){

chronometer.setBase(SystemClock.elapsedRealtime());
pauseOffset = 0;
}
public static void forwardChronometer (){
if (running) {
chronometer.setBase(chronometer.getBase()-1000);
}
}
public static void backChronometer (){
if (running) {
chronometer.setBase(chronometer.getBase()+1000);
}
}
}


MainActivity java code.



public class MainActivity extends AppCompatActivity {

public static Chronometer chronometer;
public static TextView tv_rune;
public static SeekBar sb_rune;
public static int minrune = 0, maxrune = 35, currentrune;

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


BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);

getSupportFragmentManager().beginTransaction().replace(R.id.main_frame,
new HomeFragment()).commit();

}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem
menuItem) {

Fragment selectedFragment = null;

switch (menuItem.getItemId()){
case R.id.navigation_home:
selectedFragment = new HomeFragment();
break;

case R.id.navigation_notifications:
selectedFragment = new NotificationsFragment();
break;

case R.id.navigation_info:
selectedFragment = new InfoFragment();
break;
}


getSupportFragmentManager().beginTransaction().replace(R.id.main_frame,
selectedFragment).commit();

return true;
}
};

}


Also I want to keep seekbar datas from fragment(2) to use them in fragment (1).
Actually, I wonder how can we keep data protected.










share|improve this question















I have one activity and three fragments. In fragment(1), i have a chronometer that works perfectly. Firstly, i click chronometer start button. When chronometer shows 00:10, i open fragment(2). Then i return after 6 seconds, chronometer shows 00:00. I want it to show 00:16 and keep working while i changing fragments.



Fragment (1) xml.



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>

<Chronometer
android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:textSize="70dp"
android:textColor="#fff"
/>

<Button
android:id="@+id/btn_Start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:layout_below="@+id/chronometer"
android:background="@drawable/btn_bg"
android:textColor="#fff"
/>

<Button
android:id="@+id/btn_Pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause"
android:layout_centerHorizontal="true"
android:layout_below="@+id/btn_Start"
android:background="@drawable/btn_bg"
android:layout_marginTop="20dp"
android:textColor="#fff"/>

<Button
android:id="@+id/btn_Reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:layout_centerHorizontal="true"
android:layout_below="@+id/btn_Pause"
android:background="@drawable/btn_bg"
android:layout_marginTop="20dp"
android:textColor="#fff"/>

<Button
android:id="@+id/btn_Back"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btn_Reset"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="0dp"
android:background="@drawable/btn_bg_2"
android:drawableRight="@drawable/ic_fast_rewind_black_24dp"
android:textColor="#fff" />

<Button
android:id="@+id/btn_Forward"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btn_Reset"
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
android:background="@drawable/btn_bg_2"
android:drawableLeft="@drawable/ic_fast_forward_black_24dp"
android:textColor="#fff" />

</RelativeLayout>


This is my fragment(1) java code.



public class HomeFragment extends Fragment {


private Button btn_start;
private Button btn_pause;
private Button btn_reset;
private Button btn_back;
private Button btn_forward;


@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable
ViewGroup container, @Nullable Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment_home, container, false);



chronometer = view.findViewById(R.id.chronometer);
chronometer.setFormat("%s");

btn_forward= view.findViewById(R.id.btn_Forward);
btn_back = view.findViewById(R.id.btn_Back);
btn_start= view.findViewById(R.id.btn_Start);
btn_pause= view.findViewById(R.id.btn_Pause);
btn_reset= view.findViewById(R.id.btn_Reset);

btn_back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
backChronometer();
}
});

btn_forward.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
forwardChronometer();
}
});

btn_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startChronometer();
}
});

btn_pause.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pauseChronometer();
}
});

btn_reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
pauseChronometer();
resetChronometer();
}
});

return view;
}
}


This is chronometer java code. (methods)



public class Chronometer {

public static long pauseOffset;
public static boolean running;

public static void startChronometer (){
if(!running){
chronometer.setBase(SystemClock.elapsedRealtime() - pauseOffset);
chronometer.start();
running = true;
}

}
public static void pauseChronometer (){

if (running){
pauseOffset = SystemClock.elapsedRealtime() - chronometer.getBase();
chronometer.stop();
running = false;
}

}
public static void resetChronometer (){

chronometer.setBase(SystemClock.elapsedRealtime());
pauseOffset = 0;
}
public static void forwardChronometer (){
if (running) {
chronometer.setBase(chronometer.getBase()-1000);
}
}
public static void backChronometer (){
if (running) {
chronometer.setBase(chronometer.getBase()+1000);
}
}
}


MainActivity java code.



public class MainActivity extends AppCompatActivity {

public static Chronometer chronometer;
public static TextView tv_rune;
public static SeekBar sb_rune;
public static int minrune = 0, maxrune = 35, currentrune;

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


BottomNavigationView bottomNav = findViewById(R.id.bottom_navigation);
bottomNav.setOnNavigationItemSelectedListener(navListener);

getSupportFragmentManager().beginTransaction().replace(R.id.main_frame,
new HomeFragment()).commit();

}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem
menuItem) {

Fragment selectedFragment = null;

switch (menuItem.getItemId()){
case R.id.navigation_home:
selectedFragment = new HomeFragment();
break;

case R.id.navigation_notifications:
selectedFragment = new NotificationsFragment();
break;

case R.id.navigation_info:
selectedFragment = new InfoFragment();
break;
}


getSupportFragmentManager().beginTransaction().replace(R.id.main_frame,
selectedFragment).commit();

return true;
}
};

}


Also I want to keep seekbar datas from fragment(2) to use them in fragment (1).
Actually, I wonder how can we keep data protected.







java android android-studio android-fragments






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 7 at 21:34

























asked Nov 7 at 20:52









H. Tunc

65




65












  • Can you post also the activity code?
    – Skemelio
    Nov 7 at 21:03










  • Editted. Chronometer defined in mainactivity.
    – H. Tunc
    Nov 7 at 21:35


















  • Can you post also the activity code?
    – Skemelio
    Nov 7 at 21:03










  • Editted. Chronometer defined in mainactivity.
    – H. Tunc
    Nov 7 at 21:35
















Can you post also the activity code?
– Skemelio
Nov 7 at 21:03




Can you post also the activity code?
– Skemelio
Nov 7 at 21:03












Editted. Chronometer defined in mainactivity.
– H. Tunc
Nov 7 at 21:35




Editted. Chronometer defined in mainactivity.
– H. Tunc
Nov 7 at 21:35












2 Answers
2






active

oldest

votes

















up vote
0
down vote













Try using setRetainInstance(true) in onCreate of the fragment, as described here.



https://stackoverflow.com/a/22884377/5343907






share|improve this answer





















  • It doesn't work.
    – H. Tunc
    Nov 7 at 22:32


















up vote
0
down vote













Maybe this suits your needs.



Android: How to restore the state of a stopped chronometer after rotation?






share|improve this answer























  • It did'nt work.
    – H. Tunc
    Nov 7 at 23:10










  • Could you provide a link to download your project and test it on my machine?
    – Skemelio
    Nov 7 at 23:20










  • dosya.co/mygt8k1pief6/RuneReminders.rar.html
    – H. Tunc
    Nov 8 at 12:38










  • Ok I'll take a look
    – Skemelio
    Nov 9 at 10:06










  • Did you find anything ?
    – H. Tunc
    Nov 10 at 19:52











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',
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%2f53197639%2fmy-chronometer-stops-when-i-change-the-activity-how-can-i-prevent-this%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Try using setRetainInstance(true) in onCreate of the fragment, as described here.



https://stackoverflow.com/a/22884377/5343907






share|improve this answer





















  • It doesn't work.
    – H. Tunc
    Nov 7 at 22:32















up vote
0
down vote













Try using setRetainInstance(true) in onCreate of the fragment, as described here.



https://stackoverflow.com/a/22884377/5343907






share|improve this answer





















  • It doesn't work.
    – H. Tunc
    Nov 7 at 22:32













up vote
0
down vote










up vote
0
down vote









Try using setRetainInstance(true) in onCreate of the fragment, as described here.



https://stackoverflow.com/a/22884377/5343907






share|improve this answer












Try using setRetainInstance(true) in onCreate of the fragment, as described here.



https://stackoverflow.com/a/22884377/5343907







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 7 at 22:03









Pavel B.

360210




360210












  • It doesn't work.
    – H. Tunc
    Nov 7 at 22:32


















  • It doesn't work.
    – H. Tunc
    Nov 7 at 22:32
















It doesn't work.
– H. Tunc
Nov 7 at 22:32




It doesn't work.
– H. Tunc
Nov 7 at 22:32












up vote
0
down vote













Maybe this suits your needs.



Android: How to restore the state of a stopped chronometer after rotation?






share|improve this answer























  • It did'nt work.
    – H. Tunc
    Nov 7 at 23:10










  • Could you provide a link to download your project and test it on my machine?
    – Skemelio
    Nov 7 at 23:20










  • dosya.co/mygt8k1pief6/RuneReminders.rar.html
    – H. Tunc
    Nov 8 at 12:38










  • Ok I'll take a look
    – Skemelio
    Nov 9 at 10:06










  • Did you find anything ?
    – H. Tunc
    Nov 10 at 19:52















up vote
0
down vote













Maybe this suits your needs.



Android: How to restore the state of a stopped chronometer after rotation?






share|improve this answer























  • It did'nt work.
    – H. Tunc
    Nov 7 at 23:10










  • Could you provide a link to download your project and test it on my machine?
    – Skemelio
    Nov 7 at 23:20










  • dosya.co/mygt8k1pief6/RuneReminders.rar.html
    – H. Tunc
    Nov 8 at 12:38










  • Ok I'll take a look
    – Skemelio
    Nov 9 at 10:06










  • Did you find anything ?
    – H. Tunc
    Nov 10 at 19:52













up vote
0
down vote










up vote
0
down vote









Maybe this suits your needs.



Android: How to restore the state of a stopped chronometer after rotation?






share|improve this answer














Maybe this suits your needs.



Android: How to restore the state of a stopped chronometer after rotation?







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 21:04

























answered Nov 7 at 22:10









Skemelio

49612




49612












  • It did'nt work.
    – H. Tunc
    Nov 7 at 23:10










  • Could you provide a link to download your project and test it on my machine?
    – Skemelio
    Nov 7 at 23:20










  • dosya.co/mygt8k1pief6/RuneReminders.rar.html
    – H. Tunc
    Nov 8 at 12:38










  • Ok I'll take a look
    – Skemelio
    Nov 9 at 10:06










  • Did you find anything ?
    – H. Tunc
    Nov 10 at 19:52


















  • It did'nt work.
    – H. Tunc
    Nov 7 at 23:10










  • Could you provide a link to download your project and test it on my machine?
    – Skemelio
    Nov 7 at 23:20










  • dosya.co/mygt8k1pief6/RuneReminders.rar.html
    – H. Tunc
    Nov 8 at 12:38










  • Ok I'll take a look
    – Skemelio
    Nov 9 at 10:06










  • Did you find anything ?
    – H. Tunc
    Nov 10 at 19:52
















It did'nt work.
– H. Tunc
Nov 7 at 23:10




It did'nt work.
– H. Tunc
Nov 7 at 23:10












Could you provide a link to download your project and test it on my machine?
– Skemelio
Nov 7 at 23:20




Could you provide a link to download your project and test it on my machine?
– Skemelio
Nov 7 at 23:20












dosya.co/mygt8k1pief6/RuneReminders.rar.html
– H. Tunc
Nov 8 at 12:38




dosya.co/mygt8k1pief6/RuneReminders.rar.html
– H. Tunc
Nov 8 at 12:38












Ok I'll take a look
– Skemelio
Nov 9 at 10:06




Ok I'll take a look
– Skemelio
Nov 9 at 10:06












Did you find anything ?
– H. Tunc
Nov 10 at 19:52




Did you find anything ?
– H. Tunc
Nov 10 at 19:52


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53197639%2fmy-chronometer-stops-when-i-change-the-activity-how-can-i-prevent-this%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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()