Android application goes in background after calling finish in an activity - android

I have an application with two Activitys.
The first Activity starts the second Activity on a Button click. In the second Activity, after I call finish(), also on a Button click, I expect that the application will return to the first Activity.
What happen is that the application gets minimized (goes in background). The device on which I am developing is a Sony Xperia Z2 with Android 4.4.2.
Is this an Android issue or is it something wrong that I am doing in code?
The manifest file:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.test.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.test.SecondActivity">
</application>
First activity onClick:
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivity(i);
}
});
Second activity onClick:
btn2 = (Button) findViewById(R.id.button2);
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});

First thing is that it is not a Android issue.
Secondly, please make sure you have not finished your first Activity when you are moving from First to Second.
If you are doing this in first class:
Intent in=new Intent(A.this, B.class);
startActivity(in);
this.finish();
then remove this.finish(); because it finishes up your first Activity and when you are coming back from second then first Activity is not present in the stack. So how it would be called.

First.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class First extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
}
Second.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
}

Related

i can't redirect to next registration page when i click on to SIGN IN button

basically am a java developer but now am developing an android app.In my app i can't redirect to the next page when i click on to SIGN IN button and here is my code
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
Button log,sign;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
log = (Button) findViewById(R.id.login);
sign = (Button) findViewById(R.id.signup);
log.setOnClickListener(this);
sign.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()) {
case R.id.login:
break;
case R.id.signup:
Intent open = new Intent("com.example.eblood.Register");
startActivity(open);
break;
}
}
}
}
Here is my next page java code
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 Register extends Activity{
Button backlog,regg;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
backlog = (Button) findViewById(R.id.linktologin);
regg = (Button) findViewById(R.id.register);
backlog.setOnClickListener((OnClickListener) this);
}
public void onClick (View v)
{
switch(v.getId()) {
case R.id.register:
break;
case R.id.linktologin:
Intent in = new Intent("com.example.eblood.MainActivity");
startActivity(in);
break;
}
}
}
Here is my manifest code.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.eblood"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.eblood.home"
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.eblood.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.eblood.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.example.eblood.Register"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.REGISTER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
and the error am getting is android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.example.eblood.Register }
And here is my home.java
package com.example.eblood;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class home extends Activity {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Thread timer = new Thread(){
public void run(){
try{
sleep(2000);
} catch (InterruptedException e){
e.printStackTrace();
} finally {
Intent openMainActivity = new Intent("com.example.eblood.MAINACTIVITY");
startActivity(openMainActivity);
}
}
};
timer.start();}{
}
}
Try this..
If you use Intent open = new Intent("com.example.eblood.Register"); you will get ActivityNotFoundException change it as Intent open = new Intent(MainActivity.this,Register.class);
Change this
Intent open = new Intent("com.example.eblood.Register");
startActivity(open);
to
Intent open = new Intent(MainActivity.this,Register.class);
startActivity(open);
also
change this
Intent in = new Intent("com.example.eblood.MainActivity");
startActivity(in);
to
Intent in = new Intent(Register.this,MainActivity.class);
startActivity(in);
EDIT
Change this.
Intent openMainActivity = new Intent("com.example.eblood.MAINACTIVITY");
startActivity(openMainActivity);
to
Intent openMainActivity = new Intent(home.this,MainActivity.class);
startActivity(openMainActivity);

forceclose at onclick to passing intent

I get forceclose at imagebutton on passing intent. instead of passing intent i've passed toast on that onclick it has been running successfully but the intent passed is not running successfully.
enter code here:
what's the possibilities???
package com.account;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class AccountTrackerActivity extends Activity implements OnClickListener{
ImageButton user;
ImageButton acc_list;
ImageButton add_acc;
ImageButton add_transaction;
ImageButton search_trans;
ImageButton remainder;
ImageButton recent_trans;
TextView curr_user;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
user=(ImageButton)findViewById(R.id.imageButton2);
acc_list=(ImageButton)findViewById(R.id.imageButton3);
add_acc=(ImageButton)findViewById(R.id.imageButton4);
add_transaction=(ImageButton)findViewById(R.id.imageButton6);
search_trans=(ImageButton)findViewById(R.id.imageButton7);
remainder=(ImageButton)findViewById(R.id.imageButton8);
recent_trans=(ImageButton)findViewById(R.id.imageButton5);
curr_user= (TextView)findViewById(R.id.lblcurrent_user);
user.setOnClickListener(this);
acc_list.setOnClickListener(this);
add_acc.setOnClickListener(this);
add_transaction.setOnClickListener(this);
search_trans.setOnClickListener(this);
remainder.setOnClickListener(this);
recent_trans.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
try
{
if(v.getId()==user.getId()){
Intent myIntentlogin=new Intent(AccountTrackerActivity.this,login.class);
startActivityForResult(myIntentlogin,101);
}
if(v.getId()==acc_list.getId()){
//Intent myIntentacc_list=new Intent(getApplicationContext(),acc_list.class);
Toast.makeText(getApplicationContext(),"hi....", Toast.LENGTH_LONG).show();
//startActivityForResult(myIntentacc_list,101);
//startActivity(myIntentacc_list);
}
if(v.getId()==add_acc.getId()){
Intent myIntentadd_acc=new Intent(AccountTrackerActivity.this,add_acc.class);
startActivityForResult(myIntentadd_acc,103);
}
if(v.getId()==add_transaction.getId()){
Intent myIntentadd_transaction=new Intent(AccountTrackerActivity.this,add_transaction.class);
startActivityForResult(myIntentadd_transaction,104);
}
if(v.getId()==search_trans.getId()){
Intent myIntentsearch_trans=new Intent(AccountTrackerActivity.this,search_trans.class);
startActivityForResult(myIntentsearch_trans,105);
}
if(v.getId()==remainder.getId()){
Intent myIntentremainder=new Intent(AccountTrackerActivity.this,remainder.class);
startActivityForResult(myIntentremainder,106);
}
if(v.getId()==recent_trans.getId()){
Intent myIntentrecent_trans=new Intent(AccountTrackerActivity.this,recent_trans.class);
startActivityForResult(myIntentrecent_trans,107);
}
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
to pass data from one activity
Intent myIntentlogin= new Intent(AccountTrackerActivity.this,login.class);
myIntentlogin.putExtra("message", 103); //pass data to new activity here like this
startActivity(myIntentlogin);
to receive data in another
Intent intent = getIntent();
String message = intent.getStringExtra("message");
Refer this link
Try this in your code
ImageButton ButtonOne=(ImageButton) findViewById(R.id.imageButton1);
And your onclick listener is given below
ButtonOne.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
teamsName=db.getAllTeams();
teamone=true;
Intent intent=new Intent(MainActivity.this,NextActivity.class);
intent.putExtra("item", teamsName);
startActivityForResult(intent, 0);
}
});
And you need to add your NextActivity in manifest file.
Your Mainfest is look like below
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.design.MainActivtiy"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NextActivity"
android:screenOrientation="portrait">
</activity>
</application>
</manifest>

Onclick method is not working in android Button

I was studying of Onclick events and the listeners in android. On the way, I created a sample app and my aim is to save the given number (register.java) in the database and to show it in an another activity (main.java). But, now on clicking the 'save' button, nothing has been happening. Even the toast method is also not working.
This is my code:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
data = register.this.openOrCreateDatabase("Number", MODE_PRIVATE, null);
data.execSQL("CREATE TABLE IF NOT EXISTS table1(number varchar(15));");
e1 = (EditText)findViewById(R.id.mob_num);
b1 = (Button)findViewById(R.id.save);
b2 = (Button)findViewById(R.id.go);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
number = e1.getText().toString();
data.execSQL("INSERT INTO table1 VALUES('"+number+"')");
Toast.makeText(getApplicationContext(), "'"+number+"'successfully inserted",Toast.LENGTH_SHORT).show();
Intent i = new Intent(register.this, main.class);
startActivity(i);
finish();
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i =new Intent(register.this,main.class);
startActivity(i);
data.close();
finish();
}
});
}
Here is my manifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="a.a.a"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name=".main"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE"/>
</intent-filter>
</activity>
</application>
</manifest>
I know this a very basic thing in android. But, I hope you may help me in this. Sorry and Thanks for your time.
There's an easy way to register onClickListeners in Android:
In your declaration of the button add
android:onClick="onClick" and create a method in the Activity containing the button called onClick(View v). From here you can just
switch(v) {
case R.id.save:
Toast.makeText(this, "save button has been pressed", Toast.LENGTH_LONG).show();
break;
case R.id.go:
Toast.makeText(this, "go button has been pressed", Toast.LENGTH_LONG).show();
break;
}
and so on until you've registered all the buttons in the activity. Of course you can use other variable names than what I did in this example, just remember the onClick method (or whatever you name it) MUST be called with View as parameter and the switch MUST run on that parameter.
Just try this simple approach to check button click and tell me if it works:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private Button closeButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.closeButton = (Button)this.findViewById(R.id.button);
this.closeButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Toast.makeText(this, "You clicked the button", Toast.LENGTH_SHORT).show();
}
}
Button btn,
btnback = (Button) findViewById(R.id.activity_essentials_btnback);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(v.getContext(), "Click here",
Toast.LENGTH_SHORT).show();
}
});
The Logcat provided does not give any information, Provide the correct logcat.
The code for the click listeners is right. The probable exception is only the null pointer exception. So provide null checks to all the data base creation and insertion.

Admob on Multiple Activities?

I have 7 Activities in my application. I wants to display admob in every activity
Whether i have to create each AdView in every activity?
or
is there any alternative to reuse previous activity container OR prevent it from destroy so can i use in next activity....
Any code or hint we'll b appreciate.
Thankx
I DID this. Thankx to yorkw comment. This is not an efficient code. But you can modify accordingly. That reduces your code for each activity.
Just Extends "TestingAdmobActivity" & call SetupAds() to call your advs.
My SuperClass "TestingAdmobActivity.java"
package com.test.myadmob;
import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
public class TestingAdmobActivity extends Activity implements AdListener{
public AdView adView;
public String ADV_PUB_ID = "a14e2fb60918999";
private boolean adVisible = true;
LinearLayout layout;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("Admob", "Calling External");
}
public void SetupAds(){
Log.i("AdMob", "Start Setup");
layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setGravity(android.view.Gravity.BOTTOM | android.view.Gravity.CENTER_HORIZONTAL); //To put AdMob Adv to Bottom of Screen
Log.i("AdMob", "End Layout Setup");
addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
adView = new AdView(this, AdSize.BANNER, ADV_PUB_ID);
adView.setAdListener(this);
Log.i("AdMob", "Init complete Adview");
layout.addView(adView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
Log.i("AdMob", "Done AddView Layout");
AdRequest request = new AdRequest();
request.addTestDevice(AdRequest.TEST_EMULATOR);
request.addKeyword("LifeOK");
adView.loadAd(request);
Log.i("AdMob", "End Setup");
}
private Handler handler = new Handler()
{
public void handleMessage(Message msg)
{
switch (msg.what)
{
case 0: //Disable Adv
if (adVisible)
adVisible = false;
break;
case 1: //Enable Adv
if (!adVisible)
{
Log.i("AdMob", "Case 1");
adVisible = true;
}
break;
case 2: //Enable but Hide Adv
adView.setVisibility(View.GONE);
break;
case 3: //Enable but Show Adv
adView.setVisibility(View.VISIBLE);
break;
default:
break;
}
}
};
public void DisableAds()
{
Log.i("AdMob", "Request Disable Adv");
handler.sendEmptyMessage(0);
}
public void EnableAds()
{
Log.i("AdMob", "Request Enable Adv");
handler.sendEmptyMessage(1);
}
public void HideAdv() //Enable Adv but Hide
{
Log.i("AdMob", "Request Hide Adv");
handler.sendEmptyMessage(2);
}
public void ShowAdv() //Show Adv
{
Log.i("AdMob", "Request Show Adv");
handler.sendEmptyMessage(3);
}
#Override
public void onDismissScreen(Ad arg0) {
// TODO Auto-generated method stub
Log.d("AdMob", "Dismiss Screen");
}
#Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
// TODO Auto-generated method stub
Log.d("AdMob", "failed to receive ad (" + arg1 + ")");
}
#Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
Log.d("AdMob", "Leaving Application");
}
#Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
Log.d("AdMob", "Present Screen");
}
#Override
public void onReceiveAd(Ad arg0) {
// TODO Auto-generated method stub
Log.d("AdMob", "Adv Received");
}
}
My FirstActivityClass "NewActivity_1.java"
package com.test.myadmob;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class NewActivity_1 extends TestingAdmobActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("Admob", "OnCreate");
SetupAds();
Log.i("Admob", "Done");
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("Admob", "Going to Activity 2");
Intent mainIntent = new Intent().setClass(NewActivity_1.this, NewActivity_2.class);
startActivity(mainIntent);
}
});
}
}
My SecondActivityClass "NewActivity_2.java"
package com.test.myadmob;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class NewActivity_2 extends TestingAdmobActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("Admob", "OnCreate");
SetupAds();
Log.i("Admob", "Done");
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.i("Admob", "Going Back to Activity 1");
finish();
}
});
}
}
My AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.myadmob"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".NewActivity_1" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TestingAdmobActivity" ></activity>
<activity android:name=".NewActivity_2" ></activity>
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
>
</activity>
</application>
<!-- AdMob SDK requires Internet permission -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <!-- to get Android Device ID -->
</manifest>
Note: for the sake of permissions used by google admob sdk, i have to build this on android 4.0 sdk with min-sdk version 7
In my app I have a cache of 0..12 ads at any given time. I'm reusing them accross different Fragments in an endless ViewPager. The caching class is in charge of loading the providing the ads to the Fragments.
The trick is to:
Call the AdView's onDestory only when you're sure you're done with that AdView instance for good. This means that the Fragments themselves are not in charge of this.
Passing the AdView's themselves between the Fragments, we need to remember to detach each AdView from its hierarchy:
(only on the UI thread of course):
public void detachFromHirerchy()
{
View adView = getAdView();
if ( adView != null )
{
ViewGroup parent = (ViewGroup) adView.getParent();
if (parent != null)
{
parent.removeView( adView );
}
}
}

Using Intent in an Android application to show another activity

In my Android application, I have two activity classes. I have a button on the first one and I want to show the second when it is clicked, but I get an error. Here are the classes:
public class FirstActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button orderButton = (Button)findViewById(R.id.order);
orderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(FirstActivity.this, OrderScreen.class);
startActivity(intent);
}
});
}
}
The second class that should show when the button is clicked, but never does:
public class OrderScreen extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.order);
Button orderButton = (Button) findViewById(R.id.end);
orderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
}
How do I create a button that will show the second activity?
The issue was the OrderScreen Activity wasn't added to the AndroidManifest.xml. Once I added that as an application node, it worked properly.
<activity android:name=".OrderScreen" />
Add this line to your AndroidManifest.xml:
<activity android:name=".OrderScreen" />
----FirstActivity.java-----
package com.mindscripts.eid;
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 {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button orderButton = (Button) findViewById(R.id.order);
orderButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this,OrderScreen.class);
startActivity(intent);
}
});
}
}
---OrderScreen.java---
package com.mindscripts.eid;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class OrderScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second_class);
Button orderButton = (Button) findViewById(R.id.end);
orderButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
---AndroidManifest.xml----
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mindscripts.eid"
android:versionCode="1"
android:versionName="1.0">
<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=".OrderScreen"></activity>
</application>
Use this code:
Intent intent=new Intent(context,SecondActivty.class);
startActivity(intent);
finish();
context: refer to current activity context,
please make sure that you have added activity in android manifest file.
Following code for adding activity in android manifest file
<Activity name=".SecondActivity">
</Activity>
<activity android:name="[packagename optional].ActivityClassName"></activity>
Simply adding the activity which we want to switch to should be placed in the manifest file
When you create any activity in android file you have to specify it in AndroidManifest.xml like
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".MyCreativityActivity"
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=".OrderScreen"></activity>
</application>
b1 = (Button) findViewById(R.id.click_me);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivity(i);
}
});
add the activity in your manifest file
<activity android:name=".OrderScreen" />
In the Manifest
<activity android:name=".OrderScreen" />
In the Java Code where you have to place intent code
startActivity(new Intent(CurrentActivity.this, OrderScreen.class);
you can use the context of the view that did the calling.
Example:
Button orderButton = (Button)findViewById(R.id.order);
orderButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(/*FirstActivity.this*/ view.getContext(), OrderScreen.class);
startActivity(intent);
}
});
Intent i = new Intent("com.Android.SubActivity");
startActivity(i);

Categories

Resources