I'm starting a project and I want to reproduce a video in the main activity when the app is executed, when the user presses the video it goes to another activity. If the user press the back button, he is going to the main screen again and reproduces the video from the beginning. The video is located in the raw directory.
The problem is that the videoview is reproducing the video when the activity is first created but not when the user goes back to it from the other activity (In my case the MenuSection activity). The code is really simple but i will paste it anyway:
public class MainActivity extends Activity {
private VideoView mVideoView;
LinearLayout menuSection;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() +"/"+R.raw.documentariesandyou));
mVideoView.requestFocus();
mVideoView.setMediaController(null); //i dont want the controls of the videoview.
mVideoView.start();
menuSection = (LinearLayout) findViewById(R.id.menuSection);
menuSection.setOnClickListener(new menuSectionListener());
}
class menuSectionListener implements OnClickListener {
public void onClick(View v) {
Intent staticActivityIntent = new Intent(MainActivity.this, MenuSection.class);
startActivity(staticActivityIntent);
}
}
}
The MenuSection is just an activity that shows a textview like "Hello world", so I'm not pasting it.
Move mVideoView.start(); to onResume() instead of onCreate() as:
#Override
protected void onResume() {
super.onResume();
mVideoView.start();
}
see Managing the Activity Lifecycle onResume() is called from your Activity when Activity is Already Running
call video.pause() on onPause() overrided method of your activity and call video.resume() on onResume() method of your activity.
Move mVideoView.start(); to onStart(), instead of onCreate().
See Activity Lifecycle in the developer docs for more info on how the lifecycle of Activities work.
I am not certain, but you may also need to move the setVideoURI(); to onStart() as well.
Related
I wanna know how I can avoid splash screen to launch when my app is in recents.
I want the app to open the activity where user was in it before when he/she clicks back button of device and opens
app from recent screen.
This is a splash screen activity :
public class MainActivity extends AppCompatActivity {
//variables
private static int SPLASH_SCREEN = 1000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MainActivity.this , Main_Page.class);
startActivity(intent);
finish();
}
},SPLASH_SCREEN);
}
In the manifest, specify for the activity:
android:noHistory="true"
I found the solution to my problem.
This happens because when I click back button of device, according to
activity lifecycle , ondestroyview() will be called. so when
I returned from recents screen , app started again.
what I've done is that in the next activity which appears after Splash
Screen , I used onBackPressed like this :
#Override
public void onBackPressed() {
moveTaskToBack(true);
}
I am very new to Andriod Programming and this is probably very basic question.
In my Application, the first page contains just button (for simplicity) login button.
After the user clicks login button it displays the toast and then I need to navigate to new class(page B) where I want to connect to a specific health sensor.
Problem
1. I tried implementing the just the basic part with onClickListener for button and then when clicked, go to next page, where enable Bluetooth,etc. I could not get to next page
MainActivity.java :
public class MainActivity extends AppCompatActivity {
Button button;
PollingTest pd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getBaseContext(),"Logged In",Toast.LENGTH_SHORT).show();
pd = new PollingTest();
pd.call();
}
});
}
}
Second Page (Where wanted to Control BT). Never got to this page while testing on the tablet: -
For now just included if I could get a Toast atleast from this page: -
public class PollingTest extends Activity {
BluetoothAdapter btAdapter;
Button btn2;
protected void call() {
//super.onCreate(savedInstanceState);
//setContentView(R.layout.pairinglistactivity);
btn2 = (Button)findViewById(R.id.pollB);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_LONG).show();
}
});
}
}
Here app crashes after clicking on Login button in first page.
I have actually got some different errors with different code, I was not able to make proper Toast or turn BT on in the second page as it was trying them in static method.( very Confusing:( )
Please help me. I know this v v basic Q..
EDIT:
Sorry, this Q is already answered here: -
Moving from one activity to another Activity in Android
You don't start an activity by instantiating it like a normal Java class. So this is wrong
pd = new PollingTest();
pd.call();
you should be using an Intent
and follow the Activity Lifecycle
so you would want something like
Intent i = new Intent(MainActivity.this, PollingTest.class);
startActivity(i);
then override onCreate() in PollingTest.java and put what is in call() in there or call that method from onCreate().
Also, a Toast should use Activity Context
In my application, I have two activities. First is a splash screen, which simply shows the application name and few other info.
Upon clicking on the splash screen activity, I'm loading the main activity. My app works fine, but I'm facing a small issue. If I press back button from my main activity, control is going to splash screen activity. But I don't want to show the splash screen activity again, I want to avoid splash screen activity when pressing Back button.
Is it possible? If so how?
In your AndroidManifest.xml file, add android:noHistory="true" attribute in your splash screen <activity>.
As I understand, you want the splash activity to not show after changing activity. You should note activities save On Stack and with starting new activity push on it and with finish you pop on top stack. I think that if you the call finish() method your problem fix as in your splash screen activity where you call StartActivity insert finish() after
public void onClick(View v) {
Intent intent = new Intent(Main.this, Splash.class);
startActivity(intent);
finish();
}
Hope to be useful :)
You can just call
finish();
In your Splash screen when you jump to the second screen.
In addition to the above answers, you should note
that:
1: by calling the finish() method, the Splash activity
will close after execution, meaning that it will not be
available in the stack.
#Override
protected void onCreate(Bundle saveInstsnceState){
super.onCreate( saveInstanceState);
\\ do something here
Intent intentSplash = new Intent(SplashActivity.this, NextActivity.class);
StartActivity(intentSplash);
finish ();
}
You will achieve your aim using the method above
but...
2: If you want to prevent your users from force
exiting the app (pressing back button) while the
splash activity in still ongoing which is the best
practice in android, then you need to call the
onBackPressed () method.
Class NoBackSplash{
#Override
protected void onCreate(Bundle saveInstsnceState){
super.onCreate( saveInstanceState);
\\ do something here
Intent intentSplash = new Intent(SplashActivity.this, NextActivity.class);
StartActivity(intentSplash);
finish ();
}
#Override
public void OnBackPressed(){
};
}
With this OnBackPressed() method, your splash activity will not be force to exit no matter how hard the user try.
I understand, you want the splash activity to not show when you click on back button. First of all you should know that all the activities on android are in form of STACK. So what we need we just end the splash activity after it execute. we can do this by calling finish() method in android studio. here is the solution:
Intent intent = new Intent(MainActivity.this, home.class);
startActivity(intent);
finish();
public class Splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Handler hd = new Handler();
hd.postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(Splash.this,MainActivity.class);
startActivity(i);
#by calling finish() method,splash activity will close after execution
finish();
}
},3000);
}}
I'm having a strange problem to do with stopping my android app. On my phone I have a home button and a back button, now when I go into my app after pressing the home button, the program loads data from the internet as expected, but when I go into my app after pressing the back button, the data doesn't load. I've debugged it to an extent, and have found out that the only difference is that the back button calls the onCreate() method. I'm quite confused to why this is is happening.
Here's some of my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("DAP", "Created");
setContentView(R.layout.activity_ltc);
getActionBar().setTitle("LTC Charts");
getActionBar().setLogo(
getResources().getDrawable(R.drawable.new_litecoin_logo_large));
TextView textView = (TextView) findViewById(R.id.ltcdata);
textView.setText("Loading data...");
TimerTask timer = new TimerTask() {
#Override
public void run() {
parseJSON();
}
};
Timer time = new Timer();
time.schedule(timer, 500, 85);
}
"when I go into my app after pressing the back button, the data doesn't load."
If you have already launched your app, the Activity will be paused (and onPause is called) when you navigate away from it. When you navigate back to the app, the same activity instance is resumed (and onResume is called).
See http://developer.android.com/training/basics/activity-lifecycle/index.html
This is because your activity hasnt been destroyed. What you should do is put something into the onresume of your activity to get the data again when you come back to the activity. If you want the data to be destroyed for sure and the user never leaves the activity you can destroy everything in onpause.
As others said, override onResume().
A common pattern would be to extract common initialisation code into a method, and call it from where needed:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
#Override
protected void onResume(){
super.onResume();
init();
}
private void init(){
setContentView(R.layout.activity_ltc);
getActionBar().setTitle("LTC Charts");
// ....
}
This could be because your activity is not set correctly at AndroidManifest.xml .
Make sure the activity name is the right one. If you inserted your activity into some package include that name as well . for example I have my activitu which is called "SettingsActivity" (usually the default is MainActivity) set in a package called Activities :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<activity android:name=".Activities.SettingsActivity">
...
Use the on resume function below.
#Override
public void onResume(){
//will be executed onResume
}
i want the spash screen to only show when the app has been compltely destroyed not when it is running in the background and resumed
Android's Live-Circle
When your Acrivity is created:
onCreate
onStart
onResume
when your Activity becomes inactive:
onPause
onStop
when it becomes active again:
onRestart
onStart
onResume
and when it's destroyed:
onPause
onStop
onDestroy
Edit: what i would do is, i would define a global boolean for your Main-Activity, 'showSpash' for example, and initialize it as "true". Then, when your 'onCreate'-method is first called, you set it to "false".
Then, anytime the 'onCreate'-method is called, you check if the boolean is "false". If it is, don't show the splash, if not show it.
I created global variable in my application class:
public class MyApplicationClass extends Application {
public static boolean welcomeScreenIsShown = false;
}
Then in my splash activity I did something like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
//Put this before anything else in OnCreate
if (MyApplicationClass.welcomeScreenIsShown) {
// Open your Main Activity
}
}
Then, in my Main Activity, I did:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyApplicationClass.welcomeScreenIsShown = true;
}
Good Luck!
A very simple method:
Main Activity is only a splash screen. This Activity is shown while a timer starts that elapses for say 4 seconds.
When 4 seconds hits, the splash screen activity is destroyed and the Main Application Activity is started.
Voila, you now have a splash screen that will never be shown, except when you first start the application.
public class SplashScreen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
Thread t = new Thread() {
public void run() {
try {
int time = 0;
while (time < 4000) {
sleep(100);
time += 100;
}
}
catch (InterruptedException e) {
// do nothing
}
finally {
finish();
Intent i = new Intent(SplashScreen.this, MainApplication.class);
startActivity(i);
}
}
};
t.start();
}
}
A better approach would be to set the android:noHistory="true" attribute for SplashScreenActivity in the AndroidManifest.
Isn't this the purpose of "onResume()" vs. "onCreate()"?