Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Please try below code . It will solve your prolem.</p> <p>Follow below steps.</p> <pre><code>1) Make one BaseActivity Class. 2) Make One MainActivity Class and extend BaseActivity. 3) Make another SecondActivity class and extend BaseActivity. </code></pre> <p>Here i post all the classes you required .</p> <p><strong>What i do in BaseActivity ?</strong></p> <blockquote> <p>I create one Countdown Timer class called MyCount which will display the remaining time . I create one boolean flag which set false by default . so when user call setHeading() function of BaseActivity it will check the falg if it false then start the timer and if it is true then again set the timer and starts again .</p> </blockquote> <h2>BaseActivity</h2> <pre><code>import android.app.Activity; import android.os.Bundle; import android.os.CountDownTimer; import android.widget.TextView; public abstract class BaseActivity extends Activity { private TextView textView = null; public static MyCount counter = null; private static boolean flag = false; private static long millisUntilFinishedVariable = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } protected void setHeading(String message) { if(textView == null) { textView = (TextView)findViewById(R.id.textView); if(textView != null) textView.setText(message); if(flag == false) { counter = new MyCount(30000, 1000); counter.start(); } else { counter.cancel(); counter = new MyCount(millisUntilFinishedVariable, 1000); counter.start(); } } } // countdowntimer is an abstract class, so extend it and fill in methods public class MyCount extends CountDownTimer { public MyCount(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } @Override public void onFinish() { textView.setText("done!"); //TextView object should be defined in onCreate flag = false; } @Override public void onTick(long millisUntilFinished) { millisUntilFinishedVariable = millisUntilFinished; flag = true; textView.setText("Left:" + millisUntilFinished/1000);// This will be called every Second. } } } </code></pre> <h2>MainActivity</h2> <pre><code>import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends BaseActivity { private Button buttonClick = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setHeading("Text From Main Activity"); buttonClick = (Button)findViewById(R.id.buttonClick); buttonClick.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { startActivityForResult(new Intent(MainActivity.this,SecondActivity.class), 0); } }); } } </code></pre> <h2>activity_main.xml</h2> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;include layout="@layout/header"/&gt; &lt;Button android:id="@+id/buttonClick" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="96dp" android:layout_marginTop="68dp" android:text="Second Activity" /&gt; &lt;/RelativeLayout&gt; </code></pre> <h2>SecondActivity</h2> <pre><code>import android.os.Bundle; public class SecondActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second_activity); setHeading("From Second Activity"); } } </code></pre> <h2>second_activity.xml</h2> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" &gt; &lt;include layout="@layout/header"/&gt; &lt;/RelativeLayout&gt; </code></pre> <h2>header.xml</h2> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textSize="18.0sp"/&gt; &lt;/LinearLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload