Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
XML
<?xml version="1.0" encoding="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"/>
 
    <!--<uses-permission android:name="com.google.android.things.permission.USE_PERIPHERAL_IO" />
    <uses-permission android:name="com.google.android.things.permission.MANAGE_INPUT_DRIVERS" />-->
 
 
    <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:
Java
    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() {
        // TODO Auto-generated method stub
        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() {
        // TODO Auto-generated method stub
        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:
XML
<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:
Java
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) {
						// TODO Auto-generated method stub

					}
				});
				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);
				// TextView txt = null;
				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
XML
android:clearTaskOnLaunch="true"
to AndroidManifest.

And I've tried adding to onBackPressed:
Java
moveTaskToBack(true);
and
Java
finish();
and
Java
finishAffinity();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
Posted
Updated 14-May-21 4:15am
v3
Comments
David Crow 13-May-21 22:43pm    
Are you displaying the data in a ListView or RecyclerView? If so, does that control have an associated adapter?
ynjay 14-May-21 10:05am    
Thank you!
I am displaying the data inside GridLayout, which has LinearLayout and TextView inside. There is an adapter that is connected to that xml file. I have added some more code to the question.

1 solution

Basically you need a separate object to track the current values.
Create a new class in your project.

Here's a terrible name for the thing but you get the picture.

Java
public class ValueHolder{
    public int attempts = 0;
    public String currentStatus = "none";
}


Now, on your MainActivity create a member variable and then instantiate one of those objects.

Java
private v = new ValueHolder();


Now you can use that object which will have the same lifetime as your MainActivity to keep the current values.

You can update the attempts value.
Java
v.attempts++; // increment the attempts


Your next question will be:
But how do I use that object in my second form?
You can pass values (or objects) in the bundle.
You'll have to figure that part out.
 
Share this answer
 
Comments
ynjay 14-May-21 9:59am    
Thanks so much, I can look into it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900