I want to create a WebView in onCreate() method of a derivative of Application class, then attach it to the main layout when an activity onCreate() is called and detach it when onDestroyed() is called. So, every time when an activity is being created/destroyed, the WebView component will be the same (kinda singleton). The problem is I (with my Windows API background) have no ideas how to do this. Just new WebView()/addiew()/removeView()?
Why do I want to do this, you asked? Prevent Android activity from being recreated on turning screen off In several words, the WebView should never be destroyed.
Nothing special. Register MyApp as application class name in the manifest.
public class MyApp extends Application
{
public WebView _WebView = null;
#Override
public void onCreate()
{
_WebView = new WebView(getApplicationContext());
// Settings etc.
_WebView.loadUrl("url");
super.onCreate();
}
}
Remove the view from main.xml.
public class MyActivity extends Activity
{
WebView _WebView;
RelativeLayout _Layout; // Should be declared in main.xml.
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
_Layout = (RelativeLayout) findViewById(R.id.rl);
ViewTreeObserver vto = _Layout.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new MyLayoutListener()); // .layout(0,0,width,height);
Display display = getWindowManager().getDefaultDisplay();
MyApp app = (MyApp) this.getApplication();
_WebView = app._WebView;
_Layout.addView(_WebView, display.getWidth(), display.getHeight());
}
#Override
protected void onDestroy()
{
_Layout.removeView(_WebView);
super.onDestroy();
}
}
private class MyLayoutListener implements OnGlobalLayoutListener
{
public void onGlobalLayout()
{
Display display = getWindowManager().getDefaultDisplay();
_WebView.layout(0, 0, display.getWidth(), display.getHeight());
//_Layout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
Related
I try to run a test on a fragmentDialog that is anchored to a viewId in its hosting activity
#Test
public void largeScreenDeviceUsesPopup() {
final FragmentUtilActivity fragmentActivity = new FragmentUtilActivity();
fragmentActivity.onCreate(null);
myDialogFragment = MyDialogFragment.create(FragmentUtilActivity.anchorId);
myDialogFragment.showNow(fragmentActivity.getSupportFragmentManager(), "");
assertThat(....)
}
private static class FragmentUtilActivity extends FragmentActivity {
public static int anchorId = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout view = new LinearLayout(this);
view.setId(anchorId);
setContentView(view);
}
}
however i get this error:
Your activity is not yet attached to the Application instance. You can't request ViewModel before onCreate call.
how can i fix this?
I'm a newbie here learning how to create a game in android and stumbled on a tutorial http://williammora.com/a-running-game-with-libgdx-part-1
i already finished the game but it lacks menu and score. I am getting overwhelmed by the number of classes and i don't know where to put the menu screen. there is a java class there that is named Android Launcher and i think its the one that calls the game to start so I created a layout xml menu and try to call it after pressing a button this is my code. there's no error but the game crashes
public class AndroidLauncher extends AndroidApplication {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = findViewById(R.id.btnLetsgo);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new RollOut(), config);
}
});
}
}
ive read some tutorials and its possible to create a layout for the menu. ill add another class Main.class and `public class Main extends AndroidApplication {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = findViewById(R.id.btnLetsgo);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
gotoNext();
}
});
}
private void gotoNext(){
Intent i = new Intent(this, AndroidLauncher.class);
startActivity(i);
}
}`
and the androidlauncher class
public class AndroidLauncher extends AndroidApplication {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new RollOut(), config);
Intent i = getIntent();
}
}
but its still crashing
It sounds that maybe you are very new in libgdx. It very hard to say with you where should be puth your Menu Screen. Let spend your time to study Screen Class. Which you can extend to create you MenuScreen. Here is more information I hope useful for you:
example: https://www.gamedevelopment.blog/full-libgdx-game-tutorial-menu-control/
wiki: https://github.com/libgdx/libgdx/wiki/Scene2d
class MainScreen extends Screen{ public MainScreen{} }
And in game class:
this.setScreen(new MainScreen()) ;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listen();
}
public void listen() {
Toast a = Toast.makeText(MainActivity.this,"HI",Toast.LENGTH_SHORT);
a.show();
}
}
Will this goes on printing the HI string..?
No It won't.
However, onCreate() function is called somewhat more often than you think!
(like on Screen Rotation and more ...)
Checkout Activity Lifecycle and learn when onCreate() is called.
I am designing an android application where I need to add the splash screen of my application. Generally I used to use only Activity upto till now but for this project ADT is creating the Fragment also with Activity.
Now I have a confusion where I should write code of timerTask and Timer to schedule a task to perform either in onCreate of the Activity or onCreateView method or something else ?
Currently I have written like this but I am not sure it is right or wrong.
public class SplashActivity extends Activity {
// using timer to do operation at certain 3 seconds after.
private Timer mTimer;
private TimerTask mTimerTask;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
// execute this after 3 seconds
mTimerTask = new TimerTask() {
#Override
public void run() {
// start the activity (Login/Home) depends on the login
// status
}
};
mTimer = new Timer();
mTimer.schedule(mTimerTask, 3000);
}
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_splash,
container, false);
return rootView;
}
}
#Override
public void onBackPressed() {
super.onBackPressed();
// cancel the timer if user has pressed the back button to abort it.
if(mTimer !=null)
mTimer.cancel();
}
}
where I should write code of timerTask and Timer to schedule a task to perform either in onCreate of the Activity or onCreateView method or something else ?
Create another Activity and write your timer task code and then navigate to your home activity.Do something like below,
public class MySplash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run()
{
startActivity(new Intent(MySplash.this,SplashActivity.class));
finish();
}
}, 3000);
}
}
then change you home screen code like below where you need to show your fragment class only.
public class SplashActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
Don't forget to add the MySplash in your manifest file and to make it a launcher Activity.
Note: As per the other answer, it's not recommendable to use Splash Screen unless until it is required so much.
Reference,
http://cyrilmottier.com/2012/05/03/splash-screens-are-evil-dont-use-them/
Don't include splash screens in Android. It's bad design. It ruins user experience.
Users don't like to wait. Instead, show them your normal activity and put a ProgressBar in the ActionBar or something.
If the only reason you want a splash screen is to show your logo and brand colors, you should do that in the ActionBar. Style your ActionBar to your brand colors and put the logo of your app at the left of the ActionBar.
http://developer.android.com/design/patterns/help.html#your-app
I have created HideImages() function as shown below. The problem is, that running this code causes NullPointerExcpection. When I comment out the setVisibility lines, it works fine. What am I doing wrong?
public class MainActivity extends Activity implements SurfaceHolder.Callback {
ImageView img_w0, img_w1, img_w2;
public void onCreate(Bundle savedInstanceState) {
ImageView img_w0 = (ImageView)findViewById(R.id.img0);
ImageView img_w1 = (ImageView)findViewById(R.id.img1);
ImageView img_w2 = (ImageView)findViewById(R.id.img2);
HideImages();
}
public void HideImages() {
img_w0.setVisibility(View.INVISIBLE);
img_w1.setVisibility(View.INVISIBLE);
img_w2.setVisibility(View.INVISIBLE);
}
}
Make all the references of ImageView as Global as
public class MainActivity extends Activity implements SurfaceHolder.Callback {
ImageView img_w0, img_w1, img_w2;
public void onCreate(Bundle savedInstanceState) {
img_w0 = (ImageView)findViewById(R.id.img0);
img_w1 = (ImageView)findViewById(R.id.img1);
img_w2 = (ImageView)findViewById(R.id.img2);
HideImages();
}
public void HideImages() {
img_w0.setVisibility(View.INVISIBLE);
img_w1.setVisibility(View.INVISIBLE);
img_w2.setVisibility(View.INVISIBLE);
}
}
I think this may case the problem because you had already initialize ImageView above the onCreate() method the why you declare here,
img_w0 = (ImageView)findViewById(R.id.img0);
img_w1 = (ImageView)findViewById(R.id.img1);
img_w2 = (ImageView)findViewById(R.id.img2);
The problems is here. You're already declared the ImageView objects as globally. And, again you're declaring internally in onCreate()
So, just remove the declaration inside of onCreate() and run. Like below -
public void onCreate(Bundle savedInstanceState) {
img_w0 = (ImageView)findViewById(R.id.img0);
img_w1 = (ImageView)findViewById(R.id.img1);
img_w2 = (ImageView)findViewById(R.id.img2);
}