I had read about this thread around the Internet but unlucky to find a solution. The solutions that are available did not work for me like adding the android:noHistory="true" in the Manifest file. What I wanted is that after the user clicks the play game button, which will redirect the user into the select difficulty page, when the user clicks the back button (android emulator), it will go back to the main menu.
This is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kfc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".NewKFCActivity"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".HelpPageOne"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="com.kfc.HELPPAGEONE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".HelpPageTwo"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="com.kfc.HELPPAGETWO" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".SelectDifficulty"
android:label="#string/app_name"
android:noHistory="true">
<intent-filter>
<action android:name="com.kfc.SELECTDIFFICULTY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
This is my main activity:
package com.kfc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
public class NewKFCActivity extends Activity {
/** Called when the activity is first created. */
ImageButton bPlay, bHelp;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
bPlay = (ImageButton) findViewById(R.id.playGame);
bPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), SelectDifficulty.class);
startActivityForResult(intent,0);
finish();
}
});
bHelp = (ImageButton) findViewById(R.id.Help);
bHelp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), HelpPageOne.class);
startActivityForResult(intent,0);
finish();
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
This is my difficulty class:
package com.kfc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class SelectDifficulty extends Activity{
ImageButton bEasy, bMedium, bHard;
#Override
protected void onCreate(Bundle kfcState) {
// TODO Auto-generated method stub
super.onCreate(kfcState);
setContentView(R.layout.difficultyxml);
bEasy = (ImageButton) findViewById(R.id.easy);
bEasy.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Intent intent = new Intent(v.getContext(), NewKFCActivity.class);
//startActivityForResult(intent,0);
}
});
}
}
First of all, you should know the difference between startActivity() and startActivityForResult(), see Android developers website.
Second, I think you should need to understand the lifecycle of an Activity, you don't need to finish() every activity once you leave it, Android will manage that for you.
Hope it helps :)
Cheers!
Remove the lines:
finish();
after
startActivityForResult(...);
in your Activity NewKFCActivity because finish() is destroying your activity and when you press back, there is no more an Activity to go back to!
Related
This question already has answers here:
Start Activity with Button Android
(5 answers)
Closed 8 years ago.
i am new in android development, i would like to start a new activity when i click a button, but i try so long, and dint work, please any 1 help me.
Here is the Main Project.
package com.example.pdf;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
Button tut1 = (Button) findViewById(R.id.button2);
tut1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent("com.example.pdf.main1"));
}
});
}
}
Here is my Manifest
<activity
android:name="com.example.pdf.MainActivity"
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.example.pdf.main1"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN1" />
<category android:name="android.intent.category.Default" />
</intent-filter>
</activity>
hi ,thx for all help, i already know the problem, And fix, thx for all help
Please try this
Intent I=new Intent(MainActivity.this,main1.class);
startActivity(I);
I am new here,posting my first question.So this android project about showing a layout onclick of button,isn't running.When I run it,the emulator opens but my project isn't visible on it. I am trying to show another layout when button is clicked.Can anyone help me out?
My layouts are :activity_main,tutorial1 (layout to be showed after button:tutorial1 is clicked),splash(which is showed for 5 seconds on opening).
Also manja is name of sound that is played on opening,for 5 seconds.
Here's my code:
MainActivity.java:
package com.example.thebasics;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
MediaPlayer logoMusic;
#Override
protected void onCreate(Bundle AmanIsAwesome) {
super.onCreate(AmanIsAwesome);
setContentView(R.layout.splash);
logoMusic = MediaPlayer.create(MainActivity.this, R.raw.manja);
logoMusic.start();
Thread logoTimer= new Thread (){
public void run (){
try{
sleep(5000);
Intent menuIntent=new Intent("com.example.thebasics.MENU");
startActivity(menuIntent);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
finish();
}
}
};
logoTimer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
logoMusic.release();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Menu.java:
package com.example.thebasics;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class menu extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button tut1 = (Button) findViewById(R.id.tutorial1);
tut1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent ("com.example.thebasics.TUTORIAL"));
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
}
Tutorial.java:
package com.example.thebasics;
import android.app.Activity;
import android.os.Bundle;
public class Tutorial extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial1);
}
}
}
The basics androidmanifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.thebasics"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/download"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.thebasics.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAINACTIVITY" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.thebasics.MENU"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.thebasics.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.thebasics.TUTORIAL"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.thebasics.TUTORIAL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Also the console is showing the error:- Emulator[ Warning: No DNS servers found]
whats this?
I have uploaded a fully working of your project.Download it from here.Also use Intent as other people mentioned.
Intent intent = new Intent(MainActivity.this or getApplicationContext(), Tutorial.class);
/* First argument is your current activity.You can also mention it i.e CurrentActivity.this */
/* Second argument is the class where you wanna go i.e OtherActivity.class */
startActivity(intent);
Use this instead :
startActivity(new Intent (context, Tutorial.class));
Your way of doing it is strange too :
dont do this :
<activity
android:name="com.example.thebasics.TUTORIAL"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.thebasics.TUTORIAL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Just do :
<activity
android:name="com.example.thebasics.Tutorial"
android:label="#string/app_name" >
</activity>
Same thing for the other one.
This doesn't exist :
<action android:name="com.example.thebasics.TUTORIAL" />
This line code
new Intent ("com.example.thebasics.TUTORIAL")
Creates intent with action name "com.example.thebasics.TUTORIAL"
What you want is:
startActivity(new Intent (MainActivity.this, Tutorial.class));
Just like Tsunaze suggested.
do as follow
Intent intent = new Intent(context, Tutorial.class);
// set some flags here according to your need
startActivity(intent);
Intent intent = new Intent(context, Menu.class);
// set some flags here according to your need
startActivity(intent);
and remove this from your manifest file as this is wrong
<intent-filter>
<action android:name="com.example.thebasics.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="com.example.thebasics.TUTORIAL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
When someone installs my app from the app store, the Open button (usually found next to the uninstall button upon installation completion) is grayed out, and the application cannot be found in their app drawer.
Also, when I execute from Eclipse choosing Default Activity, it says in the console that it installed the APK, but it does not start. If I select the Splash activity as the default activity in the run configuration, it runs without a hitch, but I still cannot find it in my app drawer afterwards. Help would be appreciated c:
Manifest:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.meteorfiber.gangnamstyle.Splash"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="com.meteorfiber.gangnamstyle.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.meteorfiber.gangnamstyle.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="com.meteorfiber.gangnamstyle.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Splash:
package com.meteorfiber.gangnamstyle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
public class Splash extends Activity {
private final int SPLASH_DISPLAY_LENGTH = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
}
#Override
protected void onResume() {
super.onResume();
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(this);
boolean isSplashEnabled = sp.getBoolean("isSplashEnabled", true);
if (isSplashEnabled) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Splash.this.finish();
Intent mainIntent = new Intent(Splash.this,
MainActivity.class);
Splash.this.startActivity(mainIntent);
}
}, SPLASH_DISPLAY_LENGTH);
} else {
finish();
Intent mainIntent = new Intent(Splash.this, MainActivity.class);
Splash.this.startActivity(mainIntent);
}
}
}
MainActivity:
package com.meteorfiber.gangnamstyle;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
MediaPlayer ourSong;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new GameView(this));
ourSong = MediaPlayer.create(MainActivity.this, R.raw.song);
ourSong.start();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// preventing default implementation previous to
// android.os.Build.VERSION_CODES.ECLAIR
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
}
}
Change your intent filter for your starting activity to
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
The android launcher along with the settings activity try to find this intent-filter in your application in order to launch the activity. If it can't be found, your activity cannot be launched with a typical launcher and won't be displayed.
I have created this activity with two buttons
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;
public class SelectOption extends Activity{
Button bu1,bu2,bu3;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.option_main);
bu1 = (Button)findViewById(R.id.button1);
bu2 = (Button) findViewById(R.id.button2);
bu1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myintent1 = new Intent(SelectOption.this, CheckBalance.class);
startActivity(myintent1);
}
});
bu2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myintent2 = new Intent(SelectOption.this, CheckAvailability.class);
startActivity(myintent2);
}
});
and there are 2 activities called CheckBalance.java and CheckAvailability.java also. but when I Run the program nothing happen on it. Does anyone have an idea what wrong with this??
Use this code in on click listener
Intent intent = new Intent();
intent.setClass(getApplicationContext(), CheckBalance.class);
startActivity(intent);
and also check weather you activities declare in manifest file and in Xml also....
This is My Manifest file (On behalf of Original question Asker(Anuradha))
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.people.oshada"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TheMainActivity"
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=".SelectOption"></activity>
<activity android:name=".CheckBalance"></activity>
<activity android:name=".CheckAvailability"></activity>
</application>
In ANDROID MAINFEST FILE u have to add
<activity android:name=". CheckBalance"/>
<activity android:name=". CheckAvailability"/>
I want to link my two activites by clicking on a button i have written the following code
public class IHBCAPTUREActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
ImageView iv;
TextView tx;
Button b1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
iv=(ImageView)findViewById(R.id.imageView1);
tx=(TextView)findViewById(R.id.textView1);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent calIntent;
**calIntent = new Intent(IHBCAPTUREActivity.this, LoginActivity.class);**
startActivity(calIntent);
}
}
error cumes in this line
calIntent = new Intent(IHBCAPTUREActivity.this, LoginActivity.class);
for LoginActivity.class that
LoginActivity cannot be resolved to a type . How to solve it?
here is the simple code how you can load one activity to another,
i have created two activities like this
FirstActivity.java
package com.rdc;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Button btnload = (Button) findViewById(R.id.btn);
btnload.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(this, SecondActivity.class);
startActivity(i);
}
}
SecondActivity.java
package com.rdc;
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
}
and my manifest file is
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rdc"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<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:label="#string/app_name">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
try to implement this in your app and let me know if still any doubt is there.
Check whether you have declared LoginActivity in manifest file as:
<activity
android:name="your.package-name.LoginActivity" >
</activity>
You don't have anything wrong in code.
Its know issue that occurs randomly.
solution is to set the target to other API level in project->properties->android, then set it back.then clean you project once.
I think this will refresh the .classpath or some other files, not sure, but it works.