Keeping the same background across multiple activities in Android and excluding them from activity transitions
I'm working on an Android application at the moment. Basically, I have multiple ViewFlippers which will be used to create sets of 'panels'. For example, I have a ViewFlipper to simulate a home screen environment (multiple panels that can be accessed by swiping left or right), as well as a different ViewFlipper to simulate an application launcher (panels for favorite apps, all apps, settings etc). I've done this by creating each set of panels, or each ViewFlipper in it's own activity, to preserve memory when a set of panels aren't being used.
Basically, I want to keep the same background image across all the activities. Now, I know this can be done by creating a style, and applying it in the tags in the manifest, but my issue is that the background image is included in the activity transition animation (my own custom animation has been set, but regardless, u开发者_如何学Gosing stock transition animations or not, the background image is included).
So, what I am asking is, is there a way to keep a common background image across multiple activities that is NOT animated when switching between activities?
At the moment, my style xml file looks like this:
<style name="Theme" parent="android:Theme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@drawable/background_port</item>
</style>
So as you can see, the same drawable is being set as the background for all activities, but it is included in the transition animations between activities, which I don't want. I also have tried android:detachWallpaper="true", but that seems useless in this case.
I'm fairly experienced in Android code, so no need to explain things in simple manners. If I don't understand something, I'll ask.
Thanks for all your help, it's really appreciated.
I don't think there's an easy solution for this, but something I've done in the past to get something similar happening is this; I've removed the activity transition by calling something like overridePendingTransition(0,0) just after the startActivity.
Then when the activities are about to change I've manually animated the view elements off screen, and then animated the new view elements on screen in the new activity.
Also, on a side note, if you're after a nice easy panels type view component, have a look at SwipeView https://github.com/fry15/uk.co.jasonfry.android.tools
Don't use activities. You are just making what you want to do super-complicated by trying to wrench an Activity into behaving like it is integrated like that. Use Views. You say you want to use activities to save memory, but they will not save you memory and are in pretty every way more heavy-weight than simple views. With views you have complete control going on in your UI, which is basically what you are asking for. If you want to be as efficient as possible inflate the views you want to have shown at the time you are showing them, and remove the old part of the view hierarchy that is no longer shown so it can be GCed.
Old question, but in case somebody else comes across this.
While I agree with hackbod's answer, for simple use cases there is a way to do it as of API 21.
Activity 1
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_root"
android:transitionName="activity_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/main_bg"
android:backgroundTint="#4d000000"
android:backgroundTintMode="src_over"
>
<!-- Activity 1 stuff -->
</RelativeLayout>
Activity 2
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_root"
android:transitionName="activity_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/main_bg"
android:backgroundTint="#4d000000"
android:backgroundTintMode="src_over"
>
<!-- Activity 2 stuff -->
</RelativeLayout>
Then you can start your second activity like such:
Intent startIntent = new Intent(Activity1.this, Activity2.class);
Bundle b = ActivityOptionsCompat
.makeSceneTransitionAnimation(Activity1.this,
new Pair<>(findViewById(R.id.logo), "logo"), //Or whatever other elements you don't want moving around
new Pair<>(findViewById(R.id.activity_root), "activity_root"))
.toBundle();
startActivity(startIntent, b);
精彩评论