1

I'm working on my first Android app and I don't know how to sort one issue in right way. I checked similar SO topic, for example that one but it doesn't fully explain how to set up manifest file.

I have HomeActivity, BrandActivity, MainCategoryActivity and ProductDetailsActivity it works that if you are in home one you can go to brand and from brand to main and so on. There is back button in every activity (except from home) and by clicking it it takes you to the previous one, from product to main, main to brand and brand to home.

My manifest file looks like that:

        <activity
            android:name="com.test.HomeActivity"
            android:label="@string/activity_home"
            android:launchMode="singleTask"
            android:screenOrientation="portrait" >
        </activity>
        <activity
            android:name="com.test.BrandsActivity"
            android:label="@string/activity_brands"
            android:launchMode="singleTop"
            android:parentActivityName="com.test.HomeActivity"
            android:screenOrientation="portrait" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".HomeActivity" />
        </activity>
        <activity
            android:name="com.test.MainCategoryActivity"
            android:label="@string/activity_main_category"
            android:launchMode="singleTop"
            android:parentActivityName="com.test.BrandsActivity"
            android:screenOrientation="portrait" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".BrandsActivity" />
        </activity>
        <activity
            android:name="com.test.ProductDetailActivity"
            android:label="@string/activity_product_list"
            android:launchMode="singleTop"
            android:parentActivityName="com.test.MainCategoryActivity"
            android:screenOrientation="portrait" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainCategoryActivity" />
        </activity>

Now in home, brand and main activities there is search option which takes you to ProductDetailActivity.

My question is how to make the intent from each of those activity to ProductDetailsActivity that when you press the back button in ProductDetailsActivity you go back to the previous one, so for example if you open product activity from brand you should go back from product to brand after back button is pressed, bu if you opened it from home it takes you back to home, and so on? Do I need to make some changes in manifest file and how to create intent to open ProductDetailsActivity.

And the last question what if I want to open ProductDetailsActivity from ProductDetailsActivity, How can I do it? How to make sure that back button takes me from ProductDetailsActivity to ProductDetailsActivity if it was open via ProductDetailsActivity or lets say BrandActivity if it was open view brands?

Thanks in advance.

Community
  • 1
  • 1
Greg
  • 25,317
  • 6
  • 53
  • 62

1 Answers1

0

use this code in your product details activity

     public void onClickBack(View v){
         onBackPressed();
         this.finish();
     }
 @Override
        public void onBackPressed(){
         super.onBackPressed();
     }

As onClickBack function take you back to previous activity/screen. And define back button in your xml as

   <ImageButton
        android:id="@+id/btnBack"
        android:contentDescription="@string/description_home"
        android:src="@drawable/backbuttonimg" 
        android:onClick="onClickBack"
        />
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
  • In manifest file,just define your activities and do other stuff in class file. – Giru Bhai May 09 '14 at 10:35
  • So I should remove parentActivityName and meta-data? – Greg May 09 '14 at 10:36
  • Parent activity concept is different from your requiremnt as you want to go back previous activity from productdetailactivty,no matter from where you are coming to productdetail activity. – Giru Bhai May 09 '14 at 10:39
  • And onBackPressed function handle all your requirements as back from productdetailsActivity to productdetailsActivity – Giru Bhai May 09 '14 at 10:45
  • Do you know how can I open productdetailsactivity from itself? I truing Intent productDetailsIntent = new Intent(ProductDetailActivity.this, ProductDetailActivity.class); and startActivity but it doesn't work? – Greg May 09 '14 at 10:47
  • use this code to restart activity "Intent intent = getIntent(); finish(); startActivity(intent);" But I think your requirement is different – Giru Bhai May 09 '14 at 10:52
  • I tried your code: @Override public void onBackPressed() { super.onBackPressed(); Log.d("Toolstream", "Back button pressed"); this.finish(); } I don't have custom button, I use system top-left one but it doesn't work. I added log and it looks like this method is not called at all. – Greg May 09 '14 at 10:58