Calling another activity in android - android

I am working in android development, I am trying to call another activity (activity 2) when a button is clicked
here is the code of button which is to be clicked to call the second activity
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/editText1"
android:layout_marginLeft="41dp"
android:layout_toRightOf="#+id/editText1"
android:text="Button1"
android:onClick="button1_func" />
the code of activity calss for this button
public void button1_func()
{
Intent i=new Intent ("second_activity");
startActivity(i);
}
ad here is manifest.xml
<activity
android:name="com.example.application1.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.application1.Secondactivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.second_activity" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
but my app crashes when I click the button whats the problem??

Here it is
Intent intent = new Intent(Activity1.this,Activity2.class);
startActivity(intent);

You have
android:onClick="button1_func" />
So you need
public void button1_func(View v) // change method signature
{
// do something
Intent i=new Intent (this,Secondactivity.class);
startActivity(i);
}
and remove the below for SecondActivity in manifest
<intent-filter>
<action android:name="android.intent.action.second_activity" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Read about Explicit and Implicit intents #
http://developer.android.com/guide/components/intents-filters.html

You have to use
Intent i=new Intent (MainActivity.this,SecondActivity.class);
in place of
Intent i=new Intent ("second_activity");

Related

About the XML|intent-filter

#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.ui05.ACTION_START");
intent.addCategory("com.example.ui05.MY_CATEGORY");
startActivity(intent);
}
the relevant Manifest is:
<activity android:name=".SecondActivity" >
<intent-filter>
<action android:name="com.example.ui05.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.CATEGORY" />
</intent-filter>
</activity>
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.ui05.ACTION_START");
startActivity(intent);
}
the relevent manifest is:
<activity android:name=".SecondActivity" >
<intent-filter>
<action android:name="com.example.ui05.ACTION_START" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
why the first situation would give an Exception :
android.content.ActivityNotFoundException: No Activity found to handle
Intent { act=com.example.ui05.ACTION_START cat=[com.example.ui05.MY_CATEGORY] }
The second situation is OK.
Your intent filter doesn't include the category for the category you're specifying in the intent. Change your intent filter category specifying android.intent.category.CATEGORY to com.example.ui05.MY_CATEGORY and it should work.

Main activity not starting

I have two activities in my program one where you type something into a text field then click a button to send it to another where it is displayed. Though, the one where it is being displayed shows up first.
It may be that they start at the same time, I'm not sure.
My main activity
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = " com.mycompany.myfirstapp.MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(new Intent(getApplication(), second.class));
}
public void sendMessage(View view) {
Intent intent = new Intent(this, second.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
// Do something in response to button
}
The xml file that goes with
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_send"
android:id="#+id/button"
android:singleLine="false"
android:focusableInTouchMode="true"
android:onClick="sendMessage" />
<EditText
android:layout_height="wrap_content"
android:id="#+id/edit_message"
android:hint="#string/edit_message"
android:layout_weight="1"
android:layout_width="0dp" />
</LinearLayout>
</RelativeLayout>
My android manifest
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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=".second"
android:label="#string/title_activity_second"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mycompany.myfirstapp.MainActivity" />
</activity>
</application>
My second .java file
public class second extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
Well if you're going to invoke the second.class with an intent, you're going to need an intent-filter marked inside the activity of the manifest. This is similar to that of the first class.
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
So your android manifest should look something like this:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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=".second"
android:label="#string/title_activity_second"
android:parentActivityName=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mycompany.myfirstapp.MainActivity" />
</activity>
Learn more about the Intents here.
Your line:
startActivity(new Intent(getApplication(), second.class));
starts the second activity as soon as the first one starts.
Remove this line as you are already doing what you need to in your sendMessage().

android intent filter?

I'm feeling stupid.This is very clear but I can not solve my problem.So excuse me for my question.
My problem is in about intenfilter.This is application tag of my manifest file:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".AlakyTestActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="reza"
android:name=".A2" >
<intent-filter >
<action android:name="MAIN" />
<category android:name="LAUNCHER" />
</intent-filter>
</activity>
</application>
And this is my button click listener:
b1 = (Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent2 = new Intent();
intent2.setAction("MAIN");
intent2.addCategory("LAUNCHER");
startActivity(intent2);
}
});
I think that all things is good but when I run my code and click on b1,I get this erroe:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=MAIN cat=[LAUNCHER] }
Edit:
This is A2:
public class A2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main2);
}
}
Please help me.
You should set the android:name of the second activity to the (package name).(the class)
for example, lets say the second activity class is 'com.my.app.reza' you should add you the manifest:
<activity
android:label="#string/app_name"
android:name=".reza" >
<intent-filter >
<action android:name="com.my.app.REZA" />
<category android:name="android.intent.category.DEFUALT" />
</intent-filter>
</activity>
and you should start the activity like that:
Intent intent = new Intent("com.my.app.REZA");
startActivity(intent);
NOTE that it isn't the best way to do it, you shouldn't mess to much with package name I'd recommend you to do it the following way:
<activity
android:label="#string/app_name"
android:name=".reza" />
and start it like that:
startActivity(new Intent( this.getContext() , reza.class );
Please use like that:
Intent intent2 = new Intent(context,A2.class);
startActivity(intent2);
android:name=".A2" ,you must have "A2" activity class implement!
Modify to android:name=".A2", not android:name="A2"!
You don't need to specify category if you just need to call A2 inside your app. And you should set an unique action name, for example it can be a hash string:
...
Intent intent2 = new Intent("a202bfa923069ee8e119205e3468ee131ceafda37");
startActivity(intent2);
Note that action name uses same rule as package name.
<intent-filter>
<action android:name="com.blacky.basictutorial.TutorialTwo" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Try to use this in your second activity and call by the following code:
startActivity(new Intent("com.blacky.basictutorial.TutorialTwo"));
Hope this will work for you.

Start an Activity not from an Intent

How can I start an Activity without using an Intent? The only rule I have got is
if( var == true ) startActivity();
but startActivity(); needs an Intent as a parameter.
Just create a new intent for the activity you want to start. depending on where you are you will need the app context thought.
Intent i = new Intent(getApplicationContext(), YourActivity.class);
startActivity(i);
Here's how to navigate to a second Activity (another page) using an Intent.
public void onClick(View v)
{
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
Also, don't forget to adjust the AndroidManifest.xml for each Activity.
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<activity android:name="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="SecondActivity"
android:label="#string/second_label">
<intent-filter>
<action android:name="android.intent.action.SECOND" /> //should be namespace of your company I guess
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>

Android camera intents

Roger,
I see that you've been tinkering with camera intents. I'm having real trouble just getting
a simple app to tell me when the camera button has been pressed. Do you have some code to help me on my way.
Thanks.
David
In the manifest, you need to state you want to receive the intent for the camera button:
<receiver android:name="domain.namespace.CameraReceiver">
<intent-filter>
<action android:name="android.intent.action.CAMERA_BUTTON"/>
</intent-filter>
</receiver>
<activity android:name="domain.namespace.MyCameraActivity"
android:label="#string/app_name" android:screenOrientation="landscape" android:icon="#drawable/camera"
android:clearTaskOnLaunch="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.media.action.IMAGE_CAPTURE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
In the receiver:
public void onReceive(Context context, Intent intent) {
KeyEvent event = (KeyEvent) intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null) {
return;
}
//prevent the camera app from opening
abortBroadcast();
Intent i = new Intent(Intent.ACTION_MAIN);
i.setClass(context, MyCameraActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}

Categories

Resources