Link to Innerclass for Android InView when activity resumes

I have an inner class that extends CountDownTimer. This is basically a simple countdown timer that updates the TextView in action and plays a sound when the timer ends. Code for inner class:

public class SetTimer extends CountDownTimer
{

    public SetTimer(long millisInFuture, long countDownInterval)
        {
            super(millisInFuture, countDownInterval);
        }

    @Override
    public void onFinish()
        {
            timeLeft.setText("0");
            Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Ringtone r=RingtoneManager.getRingtone(getApplicationContext(), notification);
            r.play();

        }

    @Override
    public void onTick(long millisUntilFinished)
        {
            String t;
            t=String.format("%02d:%02d", TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished), TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished)
                    -TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
            timeLeft.setText(t);
        }

}

The code that creates and references the TextView:

TextView timeLeft;

and in the onCreate method:

timeLeft=(TextView) findViewById(R.id.txtTimeLeft);

, . , TextView. TextView onCreate . , . Log.d, , onTick , . , TextView , , . TextView onTick , , TextView, . , , , SetTimer , . :

timer=new SetTimer(interval, 100);

timer.start();

, SetTimer TextView ?

+4
2

, .

, , - . , SetTimer Activity ( , Activity, ?). Activity, , , TextView, " " ".

private static class, ( ), public class ( ). , .

textview, . :

  • SetTimer:

    interface OnTickUpdateListener{
        public void onTickUpdate(String text);
    }
    
  • SetTimer :

    public class SetTimer extends ... {//this class IS IN IT OWN FILE!!!!
        private OnTickUpdateListener listener;
    
        public void registerListener(OnTickUpdateListener listener){
            this.listener = listener;
        }
        public void unregisterListener(){
             this.listener = null;
        }
    ...
    }
    
  • , :

    @Override
    public void onTick(long millisec){
        if(listener != null){
            String t;
            t = String.valueOf(millisec);//or whatever
            listener.onTickUpdate(t);
        }
    }
    
  • :

    public class MyActivity extends Activity implements SetTimer.OnTickUpdateListener{
        @Override
        public void onTickUpdate(String text){
            textView.setText(text);
    }
    

. SetTimer, . SetTimer , " ":)

  • :

    public class MyFragment extends Fragment{
    
        public static final String TAG = MyFragment.class.getSimpleName();
        private SetTimer timer;
        private static final int interval = 10;
        private MyActivity myActivity;
    
    
        @Override
        public void onAttach(Activity activity){
            super.onAttach(activity);
            this.myActivity = (MyActivity) activity;
        }
    
        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setRetainInstanceState(true);
            timer = new SetTimer(interval, 10);
            timer.start();
        }
    
        @Override
        public void onActivityCreated(Bundle state){
            super.onActivityCreated(state);
            timer.registerListener(myActivity);//activity should receive ticks
        }
    
        @Override
        public void onDetach(){
            super.onDetach();
            timer.unregisterListener();//ensure we do not post a result to non-existing  Activity
        }
    }
    
  • , , MyFragment onCreate MyActivity:

    public class MyActivity extends Activity implements SetTimer.OnTickUpdateListener{
        private MyFragment fragment;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            FragmentManager fm = getFragmentManager();
            fragment = (MyFragment) fm.findFragmentByTag(MyFragment.TAG);
            if(fragment == null){
                fragment = new MyFragment();
                fm.beginTransaction().add(R.id.container, fragment, MyFragment.TAG).commit();
    
            }
        }
    

, , MyFragment MyActivity , .

PS: , , - , , , .

+4

textview , ,

android:configChanges="keyboardHidden|orientation|screenSize"

AndroidManifest, .

MainActivity,

public class MainActivity extends Activity {

TextView timeLeft;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    timeLeft = (TextView)findViewById(R.id.textview);
    SetTimer st=new SetTimer(10000, 1000);
    st.start();
}


@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);

    return true;
}


@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.activity_main);
    timeLeft = (TextView)findViewById(R.id.textview);
}


public class SetTimer extends CountDownTimer
{

    public SetTimer(long millisInFuture, long countDownInterval)
        {
            super(millisInFuture, countDownInterval);
        }

    @Override
    public void onFinish()
        {
            timeLeft.setText("0");
            Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Ringtone r=RingtoneManager.getRingtone(getApplicationContext(), notification);
            r.play();

        }

    @Override
    public void onTick(long millisUntilFinished)
        {
            String t;
            t=String.format("%02d:%02d", TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished), TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished)
                    -TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)));
            timeLeft.setText(t);
        }

}

}

0

Source: https://habr.com/ru/post/1532507/


All Articles