On a android Studio project, when i switch to a another Activity who contains also a UnityPlayer, the UnityPlayer display a BlackScreen on the subActivity.
I am on Arcore project where i use Android studio for have a good interface and easy to implement, i use the last Unity version and Android studio, and every last android librairy(Androidx).
I needed to open the Unity view on a another activity, but do not seems to working. And every research done on google send me to answer who do not resolve my problem or else to add a android:process on my manifest.xml, but impossible to get it to work.
Every code related to mUnityPlayer
Manifest.xml of the two activity:
[...]
<activity
android:name=".MainActivity"
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density"
android:hardwareAccelerated="false"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="landscape"
android:theme="#style/AppTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
</intent-filter>
<meta-data
android:name="unityplayer.UnityActivity"
android:value="true" />
</activity>
<activity
android:name=".ViewerPlaneActivity"
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density"
android:hardwareAccelerated="false"
android:label=""
android:screenOrientation="landscape"
android:theme="#style/AppTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
</intent-filter>
<meta-data
android:name="unityplayer.UnityActivity"
android:value="true" />
</activity>
[...]
MainActivity of my application:
public class MainActivity extends AppCompatActivity {
[...]
public UnityPlayer mUnityPlayer;
public static UnityPlayerActivity currentActivity;
private boolean switchToAnAnotherUnityAct=false;
[...]
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mUnityPlayer = new UnityPlayer(this);
setContentView(R.layout.viewer_view_simple_viewer);
replaceUnityPlayerOnFrameLayout();
mUnityPlayer.getView().requestFocus();
currentActivity = this;
[...]
}
private void replaceUnityPlayerOnFrameLayout(){
FrameLayout frameLayout = findViewById(R.id.unity_player_layout);
if( mUnityPlayer.getView().getParent()==null)
frameLayout.addView(mUnityPlayer.getView());
}
#Override
protected void onDestroy() {
if(!switchToAnAnotherUnityAct)mUnityPlayer.destroy();
super.onDestroy();
}
#Override
protected void onPause() {
super.onPause();
if(!switchToAnAnotherUnityAct)mUnityPlayer.pause();
}
#Override
protected void onResume() {
super.onResume();
if(!switchToAnAnotherUnityAct){
mUnityPlayer.resume();
replaceUnityPlayerOnFrameLayout();
}
}
#Override
protected void onStart() {
super.onStart();
mUnityPlayer.start();
switchToAnAnotherUnityAct=false;
}
#Override
protected void onStop() {
super.onStop();
if(!switchToAnAnotherUnityAct)mUnityPlayer.stop();
}
#Override
public void onLowMemory() {
super.onLowMemory();
mUnityPlayer.lowMemory();
}
#Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
if (level == TRIM_MEMORY_RUNNING_CRITICAL) {
mUnityPlayer.lowMemory();
}
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mUnityPlayer.configurationChanged(newConfig);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
mUnityPlayer.windowFocusChanged(hasFocus);
}
public void onClickSizeSettings(View view) {
FrameLayout frameLayout = findViewById(R.id.unity_player_layout);
frameLayout.removeAllViews();
switchToAnAnotherUnityAct=true;
Intent intent = new Intent(this, ViewerPlaneActivity.class);
this.startActivity(intent);
}
[...]
The sub-activity:
public class ViewerPlaneActivity extends AppCompatActivity {
public UnityPlayer mUnityPlayer;
public static ViewerPlaneActivity currentActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
currentActivity = this;
mUnityPlayer= MainActivity.currentActivity.mUnityPlayer;
setContentView(R.layout.viewer_plane_unit);
FrameLayout frameLayout = findViewById(R.id.unity_player_layout);
frameLayout.addView(mUnityPlayer.getView());
mUnityPlayer.getView().requestFocus();
[...]
}
private void replaceUnityPlayerOnFrameLayout(){
FrameLayout frameLayout = findViewById(R.id.unity_player_layout);
if( mUnityPlayer.getView().getParent()==null)
frameLayout.addView(mUnityPlayer.getView());
}
#Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
if (level == TRIM_MEMORY_RUNNING_CRITICAL) {
mUnityPlayer.lowMemory();
}
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mUnityPlayer.configurationChanged(newConfig);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
mUnityPlayer.windowFocusChanged(hasFocus);
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
return mUnityPlayer.injectEvent(event);
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return mUnityPlayer.injectEvent(event);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
return mUnityPlayer.injectEvent(event);
}
public void onClickAnchorPlane(View view){
[...]
finish();
}
#Override
public void finish() {
super.finish();
FrameLayout frameLayout = findViewById(R.id.unity_player_layout);
frameLayout.removeAllViews();
}
#Override
protected void onPause() {
super.onPause();
mUnityPlayer.pause();
}
#Override
protected void onResume() {
super.onResume();
replaceUnityPlayerOnFrameLayout();
mUnityPlayer.resume();
}
#Override
protected void onStart() {
super.onStart();
mUnityPlayer.start();
}
The result than i expect is to see the UnityPlayer displayed, but i got just a blackscreen. And zero errors.
I precise than i do not get problem for return on my MainActivity where the UnityPlayer work and display correctly the viewer. Just the SecondViewer, where i got a blackscreen where i expect just to see the unity to display my plane and the camera.
Thank for your help.
For finish, i have found no solution to my problem so i have used two ViewStub on my main layout.
Related
I want to open the link https://appear.in/terochat-t192f in my custom webview in my application. When I start a new Activity that contains this webview, I am faced with this page:
And when I click on the button "join conversation", I am faced with "page not found" like this:
I found out that I should handle the URL with intent-filter in AndroidManifest.xml with:
<activity android:name=".MainSegment.Team.Team_Profile.Chatroom.VideoChatroomActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="https"
android:host="appear.in"/>
</intent-filter>
</activity>
But it has't resolved my problem yet!
This is my Java code in the activity that contains this WebView:
public class VideoChatroomActivity extends AppCompatActivity implements AdvancedWebView.Listener{
private AdvancedWebView teamChatWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_chatroom);
teamChatWebView = (AdvancedWebView) findViewById(R.id.webview_team_video_chat);
teamChatWebView.setListener(this, this);
teamChatWebView.addPermittedHostname("appear.in");
teamChatWebView.loadUrl("https://appear.in/terochat-t192f");
}
#SuppressLint("NewApi")
#Override
protected void onResume() {
super.onResume();
teamChatWebView.onResume();
}
#SuppressLint("NewApi")
#Override
protected void onPause() {
teamChatWebView.onPause();
// ...
super.onPause();
}
#Override
protected void onDestroy() {
teamChatWebView.onDestroy();
// ...
super.onDestroy();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
teamChatWebView.onActivityResult(requestCode, resultCode, intent);
// ...
}
#Override
public void onBackPressed() {
if (!teamChatWebView.onBackPressed()) { return; }
// ...
super.onBackPressed();
}
#Override
public void onPageStarted(String url, Bitmap favicon) {
}
#Override
public void onPageFinished(String url) {
}
#Override
public void onPageError(int errorCode, String description, String failingUrl) {
}
#Override
public void onDownloadRequested(String url, String suggestedFilename, String mimeType, long contentLength, String contentDisposition, String userAgent) {
}
#Override
public void onExternalPageRequest(String url) {
}
}
How can I solve this?
I added splash screen to my application and my code looks as follow:
public class SplashActivity extends AppCompatActivity {
public static final int DELAY_MILLIS = 2000;//for testing i use 5 seconds
private Handler handler = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashActivity.this, CurrencyExchangeActivity.class);
startActivity(intent);
finish();
}
}, DELAY_MILLIS);
}
#Override
protected void onStop() {
super.onStop();
handler.removeCallbacksAndMessages(null);
}
}
My Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vdovin.currencyratesapp">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name=".application.CurrencyApp"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".screens.splash.SplashActivity"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".screens.main.CurrencyExchangeActivity">
</activity>
</application>
</manifest>
So I faced with next problem:
If I hide my app with home button when loading splash screen, then when I open app again, splash screen activity not call CurrencyExchangeActivity. I understand that it appears because method onCreate() invoked only once, but I can't put it in onResume() because when I open my app again it shows me the splash screen again. But I want to show CurrencyActivity, like google's apps(maps, sheets etc...)
You cannot do that simply because you're setting the splash screen image as background of theme of your Activity. That's window level background which would show up regardless of your navigation or anything.
If you really want to show splash screen only once in app lifetime then you'll have to do it in not the right way and have that image as part of your layout and inflate that layout using setContentView. Now when you come again in the app, you will call your CurrencyExchangeActivity even before setContentView of splash activity and which will just show a black window background and directly show up CurrencyExchangeActivity.
Let me know if this makes sense or not. I can elaborate more if needed
Please try this way. I haven't tested your code. My sway of using the splash will be like below:
public class SplashActivity extends Activity {
private static final String TAG = SplashActivity.class.getSimpleName();
Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
loadNext();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Thread() {
#Override
public void run() {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(1);
}
}.start();
}
protected void loadNext() {
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
finish();
}
}
you have forgot to add
setContentView(R.layout.splash);
add this it will work
Maybe you could try the following:
#Override
protected void onResume() {
super.onResume();
Intent intent = new Intent(this, CurrencyExchangeActivity.class);
startActivity(intent);
finish();
}
This will skip the delay when reopening the app after pressing the home button.
I usually use a postDelayed Runnable.
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
#Override
public void run() {
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
finish();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
super.onResume();
// 5 sec
handler.postDelayed(runnable, 5000);
}
#Override
protected void onPause() {
super.onPause();
// 5 sec
handler.removeCallbacks(runnable);
}
}
I would like to suggest to keep the splash screen showing mechanism in the CurrencyActivity in your case. Here's the pseudo code.
I've added proper comments in the code. Please check.
public class CurrencyActivity extends AppCompatActivity {
public static final int DELAY_MILLIS = 2000; //for testing i use 5 seconds
private Handler handler = null;
private boolean showSplash = true; // True by default
private ImageView splashImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.currency_activity);
// Get a image overlaying the other views in your currency layout
splashImage = (ImageView) findViewById(R.id.splash);
if(showSplash) showSplashScreen();
}
#Override
protected void onStop() {
super.onStop();
handler.removeCallbacksAndMessages(null);
}
#Override
protected void onPause() {
super.onPause();
showSplash = false; // Set the variable to false when you take this in background
}
#Override
protected void onResume() {
super.onResume();
// Check if the variable is false and then set the visibility to GONE
if(!showSplash) splashImage.setVisibility(View.GONE);
}
private void showSplashScreen() {
handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
splashImage.setVisibility(View.GONE);
showSplash = false;
}
}, DELAY_MILLIS);
}
}
Try this out--
SplashScreenActivity-
public class SplashActivity extends AppCompatActivity {
public static final int DELAY_MILLIS = 2000;//for testing i use 5 seconds
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashActivity.this, CurrencyExchangeActivity.class);
startActivity(intent);
finish();
}
}, DELAY_MILLIS);
}
}
For manifest--
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CurrencyExchangeActivity"/>
</application>
i tested this code for if you had the problem that when the home screen button is pressed the splash screen is displayed again then this would work....
I tried to learn LocalBroadcastManager. Searched in web and implemented a simple app with Two activity to perform LocalBroadcastManager. But it's not working. I can't found what's wrong with my code. please help me!
here is my code.
FirstActivity.java
public class FirstActivity extends Activity {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
Intent intent = new Intent("localReceiver");
LocalBroadcastManager.getInstance(FirstActivity.this).sendBroadcast(intent);
}
});
} }
SecondActivity.java
public class SecondActivity extends Activity {
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
textView = (TextView) findViewById(R.id.hi_text);
LocalBroadcastManager.getInstance(this).registerReceiver(message, new IntentFilter("localReceiver"));
}
#Override
protected void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(this).unregisterReceiver(message);
}
private BroadcastReceiver message = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("SecondActivity", "Receiver Initiated...");
textView.setText("Intent receiver activated");
}
}; }
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".FirstActivity"
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=".SecondActivity"
android:windowSoftInputMode="adjustResize|stateHidden"
android:label="SecondActivity" />
</application>
you are broadcasting something from FirstActivity, and you expect that SecondActivity receives it. In order this to happen you should be able to run two different Activitys at the same time, which is, by design, not possible. By the time you broadcast the string, you don't have anybody listening for it. Try using a Service to broadcast the String, with the receiving Activity resumed, and it will work
SecondActivity would looks like this:
private BroadcastReceiver uiUpdateReceiver;
onStart():
#Override
protected void onStart() {
LocalBroadcastManager.getInstance(SecondActivity.this).registerReceiver((uiUpdateReceiver), new IntentFilter("Your_Data");
super.onStart();
}
onCreate():
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
uiUpdateReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
}
};
}
onStop():
#Override
protected void onStop() {
LocalBroadcastManager.getInstance(ChatActivity.this).unregisterReceiver(uiUpdateReceiver);
super.onStop();
}
Hope it will help you.
I am new to android and i have created a class in my android project other than the default MainActivity class and i want to start my project with that file. Here is what i added to my manifest file:
<activity
android:name="com.example.surfaceview.SurfaceViewExample"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.SURFACEVIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And the class that i created:
public class SurfaceViewExample extends Activity implements OnTouchListener {
OurView v;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
v=new OurView(this);
setContentView(v);
v.setOnTouchListener(this);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
v.pause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
v.resume();
}
public class OurView extends SurfaceView implements Runnable{
Thread t = null;
SurfaceHolder holder;
boolean isItOK=false;
public OurView(Context context) {
super(context);
holder=getHolder();
}
#Override
public void run() {
while(isItOK){
if(holder.getSurface().isValid()){
continue;
}
Canvas c = holder.lockCanvas();
c.drawARGB(255, 155, 155, 10);//canvas backgroundu boyama
holder.unlockCanvasAndPost(c);
}
}
public void pause(){ //pause the thread
isItOK=false;
while(true){
try{
t.join();
}catch(InterruptedException e){
e.printStackTrace();
}
break;
}
t=null;
}
public void resume(){ //resume the thread
isItOK=true;
t=new Thread(this); //this parameter means use this run method
//which is inside that class
t.start();
}
}
#Override
public boolean onTouch(View v, MotionEvent me) {
return false;
}
}
But the application does not start. I think the problem might be in that line inside the intent filter:
<action android:name="android.intent.action.SURFACEVIEW" />
Can anyone help me with this?
Thanks
Update your manifest with the following code for your activity. This should now launch your activity.
<activity
android:name="com.example.surfaceview.SurfaceViewExample"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I hope this helps.
I am having an issue where the application does not load after including the Splash screen to the application. This is the code for the SplashScreen.java & the Manifest file. I am not sure what I am missing. I have gone over this several times and am unable to spot the error. I have gone thro' similar posts but could find the answer. Please help
public class SplashScreen extends Activity {
private long ms=0;
private long splashTime=2000;
private boolean splashActive = true;
private boolean paused=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread mythread = new Thread() {
public void run() {
try {
while (splashActive && ms < splashTime) {
if(!paused)
ms=ms+100;
sleep(100);
}
} catch(Exception e) {}
finally {
Intent intent = new Intent(SplashScreen.this, TotalControl.class);
startActivity(intent);
}
}
};
mythread.start();
}
The Manifest file is as follows
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.tvganesh.totalcontrol.SplashScreen"
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.tvganesh.totalcontrol.TotalControl"
android:label="#string/app_name" >
</activity>
</application>
After the Splash Screen which it display it is supposed to switch to TotalControl.class. But the I get the message "The Application stopped unexpectedly"
Can you let me know what I am missing?
Don't use Thread.sleep - you have no guarantee that it will start up again exactly after 100ms. Use a Handler instead:
getWindow().getDecorView().getHandler().postDelayed(new Runnable()
{
#Override
public void run()
{
Intent intent = new Intent(SplashScreen.this, TotalControl.class);
startActivity(intent);
}
}, splashTime);
I think this will work for you
Don't use threads to show the splash.
public class SplashScreen extends Activity {
protected int _splashTime = 5000;
private Thread splashTread;
MyCount counter = new MyCount(4000, 4000);
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
counter.start();
}
public class MyCount extends CountDownTimer
{
public MyCount(long csecond, long countDownInterval)
{
super(csecond, countDownInterval);
}
#Override
public void onFinish() {
finish();
Intent intent = new Intent();
intent.setClass(SplashScreen.this, TotalControl.class);
startActivity(intent);
}
#Override
public void onTick(long arg0) {
}
}
}
I have solved the problem. This appears to be a known bug in AndEngine and the fix is add the following line to the Manifest file
android:configChanges="orientation|screenSize"
Thanks for all your help.