I'm trying to do something rather simple here, just launch a new activity from my main one. Here's the 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);
Intent i = new Intent(mainActivity.this, testActivity.class);
startService(i);
}
}
///////////////// next file /////////////////
public class testActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
Toast.makeText(this, "testActivity started", Toast.LENGTH_SHORT).show();
}
}
///////////////// manifest section ///////////////////
<application android:icon="#drawable/icon" android:label="#string/app_name">
<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=".testActivity" />
But I never see the Toast from testActivity - what gives?
You want to use startActivity instead of startService
Intent i = new Intent(mainActivity.this, testActivity.class);
startActivity(i);
To start an activity you should use startActivity() instead of startService().
You'll also have to make sure the testActivity is listed in your android manifest.
If the activity is still running in the background, it gets called and only the onResume() gets called, not the onCreate();
check the lifecycle here: http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle
Related
I'm broadcasting an intent in my app and receiving it with a broadcast receiver. I can handle the broadcasting and receiving. No problem with that. However, I want to register the receiver completely programmatically instead of doing it in the manifest file. Notice, that in the manifest file, there are two attributes of the receiver android:enabled="true" and android:exported="false". I need to know, how do I specifically set these two attributes when I register the receiver programmatically?
My AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mybroadcastapplication">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.MyBroadcastApplication">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="false">
</receiver>
</application>
</manifest>
My MainActivity.java file:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
MyBroadcastReceiver myReceiver;
IntentFilter intentFilter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myReceiver = new MyBroadcastReceiver();
intentFilter = new IntentFilter();
intentFilter.addAction("com.example.mybroadcastapplication.EXPLICIT_INTENT");
findViewById(R.id.button1).setOnClickListener(this);
}
public void broadcastIntent() {
Intent intent = new Intent();
intent.setAction("com.example.mybroadcastapplication.EXPLICIT_INTENT");
getApplicationContext().sendBroadcast(intent);
}
#Override
protected void onPostResume() {
super.onPostResume();
registerReceiver(myReceiver, intentFilter);
}
#Override
protected void onStop() {
super.onStop();
unregisterReceiver(myReceiver);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
broadcastIntent();
break;
default:
}
}
}
My MyBroadcastReceiver.java file:
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction() != null && intent.getAction().equals("com.example.mybroadcastapplication.EXPLICIT_INTENT"))
Toast.makeText(context, "Explicit intent received.", Toast.LENGTH_LONG).show();
}
}
Regards
You don't need to declare the BroadcastReceiver in the manifest if you are registering/unregistering programmatically. You only need to declare BroadcastReceivers in the manifest if you want them to be instantiated from external triggers (for example, on device boot, or from the Alarm Manager, etc.)
I'm trying to send a simply customized broadcast by clicking a button, and the receiver will push a toast as it receives the broadcast. But as I clicked the button, nothing toasted with an error message in the logcat:
2021-01-18 16:23:18.870 480-480/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2021-01-18 16:23:20.931 485-485/? E/wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument
2021-01-18 16:23:20.931 485-485/? E/wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe
The MainActivity is:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.broadcasttest2.MY_BROADCAST");
sendBroadcast(intent);
}
});
}
And the BroadcastReceiver is:
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Toast.makeText(context, "My Broadcast Received", Toast.LENGTH_SHORT).show();
throw new UnsupportedOperationException("Not yet implemented");
}
}
I have also registered the action inside the BroadcastReceiver:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.broadcasttest2">
<application
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.BroadcastTest2"
>
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.example.broadcasttest2.MY_BROADCAST" />
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Could anyone help me with that? Many thanks!
for in app broadcast communication you have to register the broadcast receiver in the Activity, in your case you missing an syntax inside onCreate like this one:
BroadcastReceiver myReceiver = new MyBroadcastReceiver();
registerReceiver(myReceiver, new IntentFilter('com.example.broadcasttest2.MY_BROADCAST'));
for more reference - Broadcast Receiver class and registerReceiver method
Hi everyone I'm studying now and developing a app in android studio. My problem is i can't proceed to my log in activity after splash screen. I already look in the net and stackoverflow and applied it, but still same error. I appreciate for the help and answer.
Here are some of my code
SplashScreen.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
final Intent mainIntent = new Intent(SplashScreen.this, Login.class);
SplashScreen.this.startActivity(mainIntent);
SplashScreen.this.finish();
}
},3000);
}
Login.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
finish();
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/logo"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".SplashScreen"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".Login"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".Login"/>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="com.ex.app.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
where did i go wrong ?
As you have written code to move to MainActivity on the method onCreate of Login.java
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
finish();
Comment this line or keep it on the click of SignIn button
Your login activity just directs you to the MainActivity on create
Look at these lines
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
It is redirecting to MainActivity directly as in your LoginActivity onCreate you have written
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
finish();
without any condition. Make it conditional by putting your login check conditions and criteria
if (<logged_in_your_condition_Check>){
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
finish();
}
why you require below lines in Login.class
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
finish();
if you are using finish() in onCreate() then it will immediately called onDestroy() and that dosen't make any sense.
remove above given code from onCreate your code will work
in manifest write:
<activity
android:name=".MySplashActivity"
android:noHistory="true"
..... >
....
</activity>
then in create() or somewhere different cal the method:
private static int SPLASH_TIME_OUT=2000;
private void nextScreen() {
new Handler().postDelayed(()-> {
Intent intent = new Intent(MySplashaActivity.this, NextActivity.class);
startActivity(intent);
finish();
}, SPLASH_TIME_OUT);
}
I've created 3 buttons. Each should open up different activities. I've tested it on my device and it never opens a new activity. Could it be due to the main.xml file where I've used the onClick feature for the button despite no using it within my main activity.
public class Main extends Activity implements View.OnClickListener{
private Button playButton, rulesButton, aboutButton;
#Override
protected void onCreate(Bundle savedInstanceState) {//when the app starts this method is run
super.onCreate(savedInstanceState);
// Set the layout for fragment_layout.xml
setContentView(R.layout.main_layout);
playButton = (Button) findViewById(R.id.button_play);
playButton.setOnClickListener(this);
rulesButton = (Button) findViewById(R.id.button_rules);
rulesButton.setOnClickListener(this);
aboutButton = (Button) findViewById(R.id.button_about);
aboutButton.setOnClickListener(this);
}
public void buttonPlayClick(){
startActivity(new Intent("com.example.will.sata.openGLActivity"));
}
public void buttonRulesClick(){
startActivity(new Intent("com.example.will.sata.DetailsActivity"));
}
public void buttonAboutClick(){
startActivity(new Intent(""));
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button_play:
buttonPlayClick();
break;
case R.id.button_about:
buttonAboutClick();
break;
case R.id.button_rules:
buttonRulesClick();
break;
}
}
}
AndroidManifest.xml
<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>
</activity>
<activity
android:name="com.example.will.sata.DetailsActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name="com.example.will.sata.openGLActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
main.layout.xml
android:id="#+id/button_play"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Play"
android:onClick="PlayGame"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
style="?android:attr/borderlessButtonStyle"
android:focusable="true" />
I have some corrections of your code, I hope it helps:
In main_layout.xml you don't need android:onClick="playGame" because you are using View.OnClickListener in the main class.
You can also use the android:onClick="playGame", but your code in the Main.class would be like this:
public class Main extends Activity {
private Button playButton, rulesButton, aboutButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
playButton = (Button) findViewById(R.id.button_play);
rulesButton = (Button) findViewById(R.id.button_rules);
aboutButton = (Button) findViewById(R.id.button_about);
}
public void playGame(View v) {
switch (v.getId())
{
case R.id.button_play:
Intent intent = new Intent(Main.this, OpenGLActivity.class);
startActivity(intent);
break;
case R.id.button_rules:
Intent intent = new Intent(Main.this, DetailsActivity.class);
startActivity(intent);
break;
}
}
To register a new activity in AndroidManifest.xml you just need to do this
<activity android:name="com.example.will.sata.OpenGLActivity"/>
<activity android:name="com.example.will.sata.DetailsActivity"/>`
To start a new activity from Main.class you have to do this (It is really important to register the Activity first in the AndroidManifest.xml):
Intent intent = new Intent(Main.this, OpenGLActivity.class);
startActivity(intent);
Intent intent = new Intent(Main.this, DetailsActivity.class);
startActivity(intent);
Tip: be careful with the naming conventions
I am pretty new to Android. I am learning about passing Intents within activities. Here is my code to pass Intent between 2 activties.
MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button)findViewById(R.id.btOk);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText etName = (EditText)findViewById(R.id.etName);
String Data = etName.getText().toString();
Intent i = new Intent("com.adhish.passingintentdata.layout2");
Bundle extras = new Bundle();
extras.putString("Name", Data);
i.putExtras(extras);
startActivityForResult(i,1);
}
});
}
layout2.java
public class layout2 extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout2);
String myName = null;
Bundle extras = getIntent().getExtras();
if(extras != null)
{
myName = extras.getString("Name");
}
TextView tvData = (TextView)findViewById(R.id.tvData);
tvData.setText(myName);
}
}
Manifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/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=".layout2"
android:label="#string/title_activity_layout2"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.adhish.passingintentdata.MainActivity" />
</activity>
When i run this code and click on the OK button to pass the data, my app crashes with a fatal error.
The error is:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.adhish.passingintentdata.layout2 (has extras) }
Please give me a detailed help about working with this issue, because I am new to Android.
Thanks.
Use com.adhish.passingintentdata.MainActivity as Action string for creating Intent to launch layout2 Activity :
Intent i = new Intent("com.adhish.passingintentdata.layout2");
and in Manifast add intent-filter for layout2 Activity:
<intent-filter>
<action android:name="com.adhish.passingintentdata.layout2" />
</intent-filter>
Use this intent instead:
Intent i = new Intent(MainActivity.this, layout2.class);