Rakeeb Rajbhandari
Rakeeb Rajbhandari

Reputation: 5063

Managing Intents between pages

In this application only after calculating/performing the onClick action you get another button to takes users into another activity.

Suppose for example I have two activities, Activity_A and Activity_B. On after placing a query in Activity_A do I get a option to go to Activity_B. In Activity_B I am using Android's navigating up function, where I am having a problem getting back to my Activity_A. The queries placed in Activity_A before I went to Activity_B is not showing, instead Activity_A is being started up as a new Activity.

The code that I am using is:

Here to reference to Activity_B I am using an onClick listener:

view_directions.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(MainActivity.this,DirectionsView.class);
            Bundle extras = new Bundle();
            extras.putString("LOCATION", location.getText().toString());
            extras.putString("DESTINATION", destination.getText().toString());
            intent.putExtras(extras);
            intent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
            startActivity(intent);

        }
    });

I also did try this removing the addFlags thing.

In Activity_B

public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId())
        {
        case android.R.id.home:
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            break;
        }
        return super.onOptionsItemSelected(item);
    }

I did also try using the addFlag(Intent.FLAG_ACTIVITY_CLEAR_TOP) here but the result that I am looking for is not being achieved.

Simply put when I refer to Activity_A from Activity_B, Activity_A should retain the queries that it generated before.

The manifest file for the application:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.calculator_taxi_fare"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:theme="@style/Theme.Sherlock.Light">
        <activity
            android:name="com.example.calculator_taxi_fare.MainActivity"
            android:configChanges="keyboardHidden|orientation"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.calculator_taxi_fare.functions.Preferences"
            android:label="@string/app_name"
            android:configChanges="keyboardHidden|orientation" >
            <intent-filter>
                <action android:name="android.intent.action.PREF" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name=".DirectionsView"
            android:label="@string/app_name"
            android:configChanges="keyboardHidden|orientation" >
            <intent-filter>
                <action android:name="com.example.calculator_taxi_fare.DIRECTIONS" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

    </application>

</manifest>

Upvotes: 0

Views: 96

Answers (2)

Muhannad A.Alhariri
Muhannad A.Alhariri

Reputation: 3912

There is more simpler solution in Manifest file set noHistory=true in activity definition

    <activity
                android:name="com.bla.bla.A_Activity"
                android:label="@string/app_name"
                **android:noHistory="true"**
                android:theme="@style/AppThemeNoTitleBar" >
</activity>

This will remove the A_Activity From activity stack automatically

Upvotes: 1

Mihir Shah
Mihir Shah

Reputation: 996

You can try this:

public boolean onOptionsItemSelected(MenuItem item)
{
    switch(item.getItemId())
    {
        case android.R.id.home:
              finish();
              break;
    }
    return super.onOptionsItemSelected(item);
}       

This will return back to activity_A and new activity_A will not be launched but you go back to the old one.

Upvotes: 2

Related Questions