1

In one of scenario, I have implemented Surface View which is part of an Activity. Now I need to hide this Video view so can roam in other screen of application.

AFAIK, we can't minimise/hide Activity, what could be other approach to handle such scenario.

I come across Hangout & WhatsApp Video calling scenario, they hide video view while move back to other activity & resume when need.

I also come across Youtube mobile application, but they might be managing everything in single activity. Also found some solution available here. & here. I still need to try.

How they do. Any suggestion !

CoDe
  • 11,056
  • 14
  • 90
  • 197
  • You Can Make One Activity contains a FrameLayout and switch fragments in it and have the Surface view above it – Ahmed Mahmoud Dec 12 '16 at 04:34
  • That's fine, but this will not be applicable for multi Activity scenario. – CoDe Dec 12 '16 at 13:10
  • Put your media player in a service/singleton. Use onSurface* callbacks of SurfaceView in to setup mediaplayer with surfaceview. You won't even notice difference even when you make a new surfaceview in a new activity. – jay shah Dec 16 '16 at 08:14
  • Fullscreen dialog? http://stackoverflow.com/questions/6329360/how-to-set-dialog-to-show-with-full-screen – Zoe Dec 17 '16 at 15:39

1 Answers1

2

Simple answer: Don't keep a view alive between activities. You can save and pass states/variables, but it is not smart to keep it in memory. So either :

  1. Switch to a fragment structure and create a fragment containing the SurfaceView attached to a View and move it to the background when not needed

  2. Or keep an instance (or the states) of the surfaceview and detach it from the attached View and reattach it when needed

I think these are the only legit ways to go for. I once did this just using a Singleton for the surfaceview and attaching/detaching it from a view when needed, but this is not the cleanest way.

Comment Pseudo code transaction

You should have the following: An Activity, FragmentA, FragmentB and VideoFragment. The Activity layout only consists about two viewholders, one that is full screen (for FragmentA and B) and the other one that is for the video view. To create fragments just right click New->Fragment->Blank. First you'll have to init the fragment in the view to do this and eventually replace FragmentA with FragmentB you could use the following code:

Fragment fragmentB = new FragmentB();

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.viewHolder, fragmentB);
transaction.commit();

Your activity layout should look something like this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="nl.coffeeit.clearvoxnexxt.activities.TestActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <FrameLayout
            android:id="@+id/viewHolderFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#F00"/>

        <FrameLayout
            android:id="@+id/videoHolder"
            android:layout_width="300dp"
            android:layout_height="200dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_margin="16dp"
            android:alpha="0.5"
            android:background="#00F"/>
    </RelativeLayout>


</android.support.design.widget.CoordinatorLayout>
jobbert
  • 3,297
  • 27
  • 43
  • 1
    Though I'm still not to sure if solution work OR not. But thanks for your answer & encouragement. – CoDe Dec 20 '16 at 04:02
  • I hope that I helped, if you need any help let me know. – jobbert Dec 20 '16 at 09:47
  • Thanks. You saw my case. I have continue to show Surface View somewhere in screen even if user is moving from Activity to Activity. I wanted to minimise like how Youtube guy did it. My Surface View has something with remote media streaming. Any suggestion over it. – CoDe Dec 21 '16 at 06:44
  • You should really use fragments, otherwise the switch between activities will always be visible in the video frame. – jobbert Dec 21 '16 at 07:56
  • Not following you, will it be possible to create an pseudo sample flow you talking about – CoDe Dec 21 '16 at 14:56
  • You should have the following: An Activity, FragmentA, FragmentB and VideoFragment. The Activity layout only consists about two viewholders, one that is full screen (for FragmentA and B) and the other one that is for the video view. When you switch fragments just call the code added bottom of solution. – jobbert Dec 21 '16 at 14:59