I have this wierd problem (at last for me). I am a new android developer so forgive me my ignorance.
Here's the description:
I have Two activities: ActivityA and ActivityB
ActivityA is main activity to be extended by other activities. It contains common layout such as common header and footer. In header there is TextView object that is supposed to display date and time. There is also a timer that refreshes every 1 second and displays date and time in Textview object. It works if i run ActivityA on it's own.
ActivityB extends ActivityA. ActivityB also uses INCLUDE XML directive to reuse the layout of ActivityA. So far so good, the layout is reused in both design time and run time and i can see reused layout and TextView.
Now the problem:
When i start ActivityB (which extends ActivityA and includes it's layout) activity shows on emulator screen but TextView that is supposed to show the date and time is not refreshing even thoug the timer works in ActivityA and sets the data (i can observe it in the debugger). It just dont propagate the changes to ActivityB's layout. Is it supposed to work this way?
What i Want to achieve:
I want to have one common Activity. the other activities would reuse it's layout and extend it. I want the code that works in common activity changes the objects in all the activites that extends it. For example i want date and time to be shown on every Activity that extends layout and code of the common activity. I hope i was clear enough.
Here's the code:
public class ActivityA extends AppCompatActivity { public TextView timeText; public TextView dateText; public TextView prijavljenText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.base_activity); timeText =(TextView) findViewById(R.id.txtTime); dateText =(TextView) findViewById(R.id.txtDate); Timer timer = new Timer(); timer.schedule( new TimerTask() { @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { Timer_Elapsed(); } }); } },0,1000); Button btn = (Button) findViewById(R.id.btnClose); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } public void Timer_Elapsed(){ Date d=new Date(); String time= new SimpleDateFormat("HH:mm:ss").format(d); String date= new SimpleDateFormat("dd.MM.yyyy").format(d); dateText.setText(date); timeText.setText(time); } } public class ActivityB extends ActivityA{ public ActivityAmyBase=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_a); }); } } Here's ActivityB XML layout <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" tools:context=".ArhivRacunaActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Logiran" android:id="@+id/btnSetLog" android:layout_marginBottom="183dp" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" /> <include layout="@layout/activity_a" /> </RelativeLayout>
No comments:
Post a Comment