I'm new with Android.
I have an splash activity with a simple logo image and running this code on the .java class:
public class Splash extends Activity {
private int SPLASH_TIEMPO = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
Intent intent = new Intent(getApplicationContext(), Login.class);
startActivity(intent);
finish();
overridePendingTransition(R.anim.anim_in_fade, R.anim.anim_out_fade);
}
}, SPLASH_TIEMPO);
}
}
Then, In my Login.class i don't have any code:
public class Login extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
}
}
In my login layout I made some designs. But when I run the application on my phone, It always shows the Hello world! text example:
What do you thing could be wrong?
Edit: In a debug mode, I see that the code to show the Login Activity is executing.
Related
In a project there will be lots of pages(activities) and user will be able to switch between these pages (activities). So when user press the corresponding button that opens page_2 from page_1, I need to create a new Activity. However, if user comes back to page_1 and try to open the page_2 again, there will be a new Activity created again, instead of opening the previously created activity ( I want user to see the page_2 as he/she left it without anychanges). So I want to put something like
if(SecondActivity==null)
{
//Create new activity
}
start(new_activity);
Here is the corresponding code ( I couldn't implement onClickListener because I couldn't disable it in onPause() method... so I used onClick from xml)
public class MainActivity extends Activity {
private View.OnClickListener openSecondPage = new View.OnClickListener() {
#Override
public void onClick(View v) {
Button button_newPage = findViewById(R.id.button_newpage);
button_newPage.setText("Clicked");
Intent secondPage = new Intent(getApplicationContext(), SecondActivity.class );
startActivity(secondPage);
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onResume(){
super.onResume();
//Button button_newPage = findViewById(R.id.button_newpage);
// button_newPage.setOnClickListener(openSecondPage);
}
public void onPause(){
super.onPause();
//Destroy the on click listener
Button button_newPage = findViewById(R.id.button_newpage);
// button_newPage.setOnClickListener(null);
}
public void openSecondPage(View v)
{
Intent secondPage = new Intent(getApplicationContext(), SecondActivity.class );
startActivity(secondPage);
}
}
Edit: Here is the new code with Flags:
MainActivity.java code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onResume(){
super.onResume();
}
public void onPause(){
super.onPause();
}
public void openSecondPage(View v)
{
Intent secondPage = new Intent(getApplicationContext(), SecondActivity.class );
secondPage.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
startActivity(secondPage);
}
}
SecondActivity.java code:
public class SecondActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondactivity);
}
protected void onResume()
{
super.onResume();
}
public void goBack(View v)
{
}
public void goMainPage(View v)
{
Intent mainPage = new Intent(getApplicationContext(),MainActivity.class);
mainPage.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
startActivity(mainPage);
}
}
I think you can resolve problem by FLAG_ACTIVITY_REORDER_TO_FRONT flag.
You need to set flag when start activity.
public void openSecondPage(View v) {
Intent secondPage = new Intent(getApplicationContext(), SecondActivity.class);
secondPage.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(secondPage);
}
You can find more detailed information in this link.
https://developer.android.com/guide/components/activities/tasks-and-back-stack
Show Activity A, then when you need it then show Activity B, then when you need it then show Activity A again, by calling startActivity + setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) => Activity A will show up as it was exactly before showing Activity B
According to your question asked, it seems you have to use either Room or SharedPreference in SecondActivity to save the user input data.
So, whenever you come back to SecondActivity, check if there is already data available. If there is a data available, show it to the user else set the fields with default values.
i just need to know how is it possible to create a start-up animation in your app. When the app is launched I would like it to go through custom animation and then it reaches the content of the app (main activity) ...
Thanks for all your help
If you really want a "startup" screen, just load up another activity before the MainActivity and display that screen for x amount of time:
public class SplashScreen extends Activity {
// Splash screen timer
private static int TIME_OUT = 5000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, TIME_OUT);
}
}
I want to show launcher screen in Android app only once. Then if the user is on the second screen, if he presses back button, I want app to close. What's wrong in this code? The first screen shows again, what mustn't be.
public class MainActivity extends Activity {
private boolean firstscreenshown=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
if (firstscreenshown==true) finish();
firstscreenshown=true;
or
public class MainActivity extends Activity {
private boolean firstscreenshown;
public MainActivity() {
this.firstscreenshown = false;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
if (firstscreenshown==true) finish();
firstscreenshown=true;
Use this code for handle back button in your MainActivity class:
#Override
public void onBackPressed()
{
// TODO Auto-generated method stub
super.onBackPressed();
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}
I have a disclaimer view that I display the first time my app is run. Here is how I handle it:
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// check preferences to see if disclaimer has been display
boolean showDisclaimer = getPreferences(MODE_PRIVATE).getBoolean("disclaimer", true);
if (showDisclaimer) {
// turn off the disclaimer
getPreferences(MODE_PRIVATE).edit().putBoolean("disclaimer",false).commit();
// display the disclaimer
Intent intent = new Intent(MainActivity.this, LegalActivity.class);
startActivity(intent);
}
setContentView(R.layout.activity_main);
}
Here is the activity for the disclaimer:
public class LegalActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.legal_detail);
// Watch for guide button clicks.
Button button = (Button) this.findViewById(R.id.legal_button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
My disclaimer view has a done button which closes it.
Hope this helps!
Call finish after your call to the second activity, so when the second screen appears the previous one will be erased.
startActivity(intent)
The best and simplest way to achieve this would be to override onPause() method and call :
finish();
inside the first activity!
My app has the following Activities:
DashboardActivity
SplashActivity
MainActivity
This how the flow is required:
The dash board has a button that starts up the MainActivity. The onCreate() method of the main activity checks if a user-profile was previously created, if yes, then it starts normally (this is fast, no GUI delay).
If no user-profile is found, then it's required to display a splash screen having instructions/how-to for the mainActivity, meanwhile the onCreate() of the main activity creates a new user-profile file(slow and GUI blocking).
What I'm currently see is the splash/instructions showing delayed after the slow user-profile creation ends.
Here is a code snippet from MainActivity.
private void showUsage(){
Thread splashTread = new Thread() {
public void run() {
try {
Intent instructionIntent = new Intent(MainActivity.this,
InstructionsActivity.class);
startActivity(instructionIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
};
splashTread.start();
}
#Override //MainActivity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
if (!IsProfileExists()){
showUsage();
try{
createUserProfile(); //slow!
} catch (Exception e) { }
}
/*Continue with MainActivity*/
}
The Splash dismisses itself by a click:
public class InstructionsActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.instructions_layout);
ImageView instructions = (ImageView) findViewById(R.id.ivInstructions);
instructions.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
The problem:
The Instructions Activity (splash) shows after the GUI gets block from the MainActivity onCreate().
Any clues?
Do the condition check (IsProfileExists()) from dashBoardActivity and call the Splash activity if profile doesn't exist.
I have an Android App That requires a button to open up a new xml page. This is what it's like now, could someone add the necessary code to make it open Page2Activity when I click on the button? Code:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Void onClick;View arg0; {
// TODO Auto-generated method stub
}
};
}
I figured this out using this method : http://stackoverflow.com/questions/4094103/linking-xml-pages-with-layout ,but I will try All of yours as well.
Try this code:
public void handleClick(View v){
//Create an intent to start the new activity.
Intent intent = new Intent();
intent.setClass(this,Page2Activity.class);
startActivity(intent);
}
Then create a new cLass called Page2Activity.
Hope this helps and don't forget to add your activity to the manifest file.
i think you mean something like this:
public class MyClass extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent("com.myaction");
startActivity(i);
}
});
}
}