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().
Related
I have 3 activities in my application.
Start activity(with a timer of 5 sec after that Menu activity appears)
Menu activity
Main activity
The Menu Activity has a listview adapter.
I want to get the Main activity after clicking an item form the list but it is not switching to Main Activity after clicking.
Here is my Menifest file
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="THE APP"
android:theme="#style/AppTheme" >
<activity
android:name=".start"
android:theme="#style/ThemeOverlay.AppCompat.Dark">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter android:label="MAIN">
<action android:name="androphlie.myfirst.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Menu"
android:label="MENU" >
<intent-filter
android:label="MENU" >
<action android:name="androphlie.myfirst.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
The Menu.java is as follows
public class Menu extends ListActivity {
String classes[]={"MainActivity","example1","example2","example3","example4","example5"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String listitem=classes[position];
try {
Class ourClass = Class.forName("com.androfile.myfirst."+listitem);
Intent ourIntent = new Intent(Menu.this, ourClass);
startActivity(ourIntent);
}catch (ClassNotFoundException e){e.printStackTrace();
}
}}
I am new to android and not able to find out the problem in code
In your onListItemClick listener ,package name com.androfile.myfirst you entered is incorrect compared to manifest file com.androphlie.myfirst
Change
Class ourClass = Class.forName("com.androfile.myfirst."+listitem);
to
Class ourClass = Class.forName("com.androphlie.myfirst."+listitem);
So I was trying to work on graphics and wanted to use intent to switch between activities and its not the first time I am doing it but I really cannot find the error. So I am posting the code for my mainactivity and manifest here. Please tell me the cause of the problem
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent p = new Intent("com.example.menuinflater.GFX");
startActivity(p);
}
});
}
and Manifest looks 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=".About"
android:label="#string/title_activity_about"
android:theme="#android:style/Theme.Dialog" >
<intent-filter>
<action android:name="android.intent.action.About" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Prefs"
android:label="#string/title_activity_about" >
<intent-filter>
<action android:name="android.intent.action.Prefs" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".GFX"
android:label="#string/title_activity_gfx" >
</activity>
</application>
I hope that makes clear where am I going wrong and I would want an answer with explanation. I would highly appreciate any solution.
Thanks in advance!
I suggest reading the basic training pages on developer.android.com, they contain alot of basic and useful information.
About your specific problem, this link ought to help: http://developer.android.com/training/basics/firstapp/starting-activity.html
The intent should be like this:
Intent p = new Intent(this, GFX.class);
I was trying to get a small image to show up before my Main Activity starts. This is my current coding in the android manifest
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
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=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.test.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
whatever I do, my splash screen does not start. They start separately, but never together, (I still haven't put a timer in my splash image as I want to check whether it works or not and it isn't working)
Remove
<intent-filter>
<action android:name="com.example.test.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Use below code in splash screen after desired time
Intent intent=new Intent(this,MAINACTIVITY.class);
startActivity(intent);
finish(); //To close splashscreen when MAINACTIVITY loads
The above code starts with splash screen and after some time start your main activity
You should remove the <intent-filter> section from your MainActivity declaration ,
and launch the MainActivity from the splashActivity using a simple intent and startActivity call.
Try this.
<activity
android:name=".Splash"
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=".MainActivity" >
</activity>
</application>
Code for your Splash Class-
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
public class Splash extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{ // TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final Handler handle = new Handler();
Runnable delay = new Runnable() {
public void run() {
startActivity(new Intent(Splash.this, MainActivity.class));
finish();
}
};
handle.postDelayed(delay,5000);
}
}
its delay next intent 5 second. you can set time according to you.
Use This:
<activity
android:name=".Splash"
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=".MainActivity" >
</activity>
</application>
and This :
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
Intent mInHome = new Intent(Splash.this, MainActivity.class);
Splash.this.startActivity(mInHome);
Splash.this.finish();
}
}, 3000);
}
}
When I start the AndARActivity as the main activity, the camera is activated and the ARObjects will be drawn correctly. If I call the AndARActivity from another, normal Activity, the camera is activated, but the ARObjects will not be drawn.
Why aren't the ARObjects being drawn?
BreakoutARActivity.java
public class BreakoutARActivity extends AndARActivity
main.xml
<ImageButton
android:id="#+id/play"
style="#style/dashButtonStyle"
android:layout_marginTop="20dp"
android:contentDescription="#string/play"
android:onClick="play"
android:src="#drawable/play" />
manifest.xml
<activity
android:label="#string/app_name"
android:name=".BreakoutLauncherActivity"
android:screenOrientation="landscape">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="#string/app_name"
android:name=".BreakoutARActivity"
android:screenOrientation="landscape">
<intent-filter >
<action android:name="ch.bfh.ti.breakoutar.activity.BreakoutARActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
BreakoutLauncherActivity.java
public class BreakoutLauncherActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
}
public void play(View view){
Intent intent = new Intent(this, BreakoutARActivity.class);
intent.setAction(Intent.ACTION_VIEW);
startActivity(intent);
}
I have 2 activities with a few buttons etc. I want to start new activity. I have done everything the tutorial said, but the 2nd activity does not start!
case R.id.btnIstorija:
Intent i = new Intent (this,KlasaPrikazBaze.class);
startActivity(i);
break;
It should start my 2nd activity
public class KlasaPrikazBaze extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.prikazbaze);
TextView TV = (TextView)findViewById(R.id.tvSQLinfo);
KlasaBaze info = new KlasaBaze(this);
info.open();
String podatak = info.DohvatiPodatak();
info.close();
}
}
However nothing happens.
Manifest:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".TalentiFinalActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".KlasaPrikazBaze"></activity>
</application>