I have an android app which turns on a sensor on my device and collects and displays data in the app to the user.
On the home screen the user presses "START" which opens a new page that displays the data.
When the user presses the back button it take them back to the home page.
The problem I am facing is when the user presses "START" again, the display page still shows values from the previous test.
Here is the AndroidManifest:
="1.0"="utf-8"
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/company"
android:label="Company"
android:theme="@style/TempTheme" >
<activity
android:clearTaskOnLaunch="true"
android:name="MainActivity"
android:label="Company" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Part of the MainActivity code:
private void showFragment(boolean show, String tag) {
PlaceholderFragment fragment = (PlaceholderFragment) getFragmentManager().findFragmentByTag(tag);
final FragmentTransaction loadTransaction = getFragmentManager().beginTransaction();
View fragmentContainer = findViewById(R.id.fragmentContainer);
if (show) {
mCurrentTag = tag;
} else {
mCurrentTag = null;;
}
if (!show && FRAGMENT_TAG_RESULTS.equals(tag)) {
if (!(TextUtils.isEmpty(mHR) || TextUtils.isEmpty(mBP)
|| TextUtils.isEmpty(mMaxHP) || TextUtils.isEmpty(mMinHP))) {
final SharedPreferences sh = getSharedPreferences(TEST_LOG_PREF, MODE_PRIVATE);
sh.edit().putString(formatDate(new Date(), "yyyy-MM-dd-HH:mm:ss"),
mTemp).apply();
mTemp = "";
}
}
if (fragment == null && show) {
fragment = new PlaceholderFragment(tag);
loadTransaction.replace(R.id.fragmentContainer, fragment, tag);
getFragmentManager().executePendingTransactions();
}
if (show) {
fragmentContainer.setVisibility(View.VISIBLE);
loadTransaction.show(fragment);
} else {
loadTransaction.hide(fragment);
fragmentContainer.setVisibility(View.GONE);
}
loadTransaction.commitAllowingStateLoss();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hrbp_main);
findViewById(R.id.btn_test_temp).setOnClickListener(this);
mWrist = (TextView) findViewById(R.id.txt_wrist);
mTemp = (TextView) findViewById(R.id.txt_temp);
ClearHistory();
}
@Override
protected void onNewIntent(Intent intent) {
Log.i("DHYCO", "onNewIntent:"+intent);
super.onNewIntent(intent);
}
@Override
public void onBackPressed() {
if (mCurrentTag != null) {
showFragment(false, mCurrentTag);
enableSensorOrNot(false);
startFlashSensor(false);
ClearHistory();
serviceconnection.StopCount();
mFlashThread = null;
} else
super.onBackPressed();
}
private void ClearHistory() {
Tempresult = -1;
Tempcircle = (ImageView) findViewById(R.id.Tempcircle);
}
@Override
protected void onStop() {
super.onStop();
ClearHistory();
enableSensorOrNot(false);
startFlashSensor(false);
serviceconnection.StopCount();
mFlashThread = null;
}
Any ideas would be great, thanks so much!
Update
Code for design of data display:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="1"
android:background="@color/layout_backgroud"
android:padding="5dp"
android:paddingRight="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/layout_backgroud"
android:orientation="horizontal">
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/temp"
android:background="@color/layout_backgroud"
android:textSize="25sp" />
<TextView
android:id="@+id/txt_temp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="end"
android:paddingStart="4dp"
android:textSize="25sp"
android:background="@color/layout_backgroud"/>
<Space
android:layout_width="20dp"
android:layout_height="match_parent"
android:background="@color/layout_backgroud"/>
<ImageView
android:id="@+id/Tempcircle"
android:layout_width="32dp"
android:layout_height="match_parent"
android:background="@drawable/circle"/>
</LinearLayout>
Adapter in MainActivity:
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
private HPAdapter mHisAdapter;
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
mHisAdapter = null;
mHisAdapter = new HPAdapter(getActivity(), R.layout.record_item);
lv.setAdapter(mHisAdapter);
} else if (FRAGMENT_TAG_RESULTS.equals(mTag)) {
mHisAdapter = null;
rootView = inflater.inflate(R.layout.results_main, container, false);
mTemp = (TextView) rootView.findViewById(R.id.txt_temp);
class HPAdapter extends ArrayAdapter<ViewHolder> {
private final int mResourceId;
public HPAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
mResourceId = textViewResourceId;
}
What I have tried:
I have tried adding
android:clearTaskOnLaunch="true"
to AndroidManifest.
And I've tried adding to onBackPressed:
moveTaskToBack(true);
and
finish();
and
finishAffinity();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);