Вопрос

I have simple android application with two activities:

  • First main activity containts ViewPager with tab navigation (3 tabs, one fragment per tag). One of these fragments is ListFragment which is a contact book (images and text).

  • Second activity represents contact details.

When user clicks on listview contact row then the second activity is started with contact details. Back buttom from this activity works fine, when clicked then main acitivity is resumed immediately. But I have problem with home button (up button in action bar). When it's clicked then main activity is not resumed but created again and it consume lot of time (even 800ms).

Manifest card activity config with home button fragment:

<activity
     android:name="com.sampleapp.activity.CardActivity"
     android:label="@string/title_activity_card"
     android:parentActivityName="com.sampleapp.activity.MainActivity">
     <!-- Parent activity meta-data to support API level 7+ -->
     <meta-data
          android:name="android.support.PARENT_ACTIVITY"
          android:value="com.sampleapp.activity.MainActivity" />
</activity>

Is there any way to resume main acitivity from stack (if it is there) when home button occur?

Это было полезно?

Решение

Try This It may help you to get activity in front

Intent i = new Intent(this,YourActivityName.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);

Другие советы

The FLAG_ACTIVITY_CLEAR_TASK flag will probably behave better then the accepted answer's flag:

Intent i = new Intent(this,YourActivityName.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);

From the docs:

If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top