Switch Screens in Android not working - java

I have a "Play Now" button for a simple android game. When I click the button it calls start, but it doesn't do anything.
Here is start():
public void start(View view) {
Intent myIntent = new Intent(this, Game.class);
startActivity(myIntent);
}
and Game.java:
public class Game extends MainActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
}
Also, I didn't forget to put it into the manifest
<activity android:name=".Game"></activity>
I'm new to android and this is all very confusing. I tried putting an intent filter although I probably did it wrong.
I looked at this How to switch between screens? but it didn't work for me.

You are finishing the activity just when you create it (onCreate). Try deleting or commenting finish(); and good luck!

remove following lines, we use them with startActivityForResult , after removing it should work other than this everything is fine
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();

Actually,your start function is working fine.But the problem is with onCreate() method in Game activity.You are calling finish() method in this which is killing the activity.Get rid of this method and then check.One thing more,I don't understand what is the purpose of setResult in your context.It is actually used for startActivityForResult() method.Refer to this link for further information:
https://developer.android.com/training/basics/intents/result.html
public class Game extends MainActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
//Intent intent = new Intent();
//setResult(RESULT_OK, intent);
//finish();
}
}

Related

ActivityNotFoundException: No Activity found to handle Intent { (has extras) }

I have an activity, let's call it MainActivity which has an SomeAdapter.
In the adapter's code I have
#Override
public void onBindViewHolder(#NonNull OptionViewHolder holder, final int position) {
final Option o = values.get(position);
holder.textView.setText(o.getOption());
holder.foreGround.setBackgroundColor(o.getOptionLayout().getBackGroundColor());
holder.editOptionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.putExtra("option",o);
context.startActivityForResult(intent,1);
}
});
}
When actually clicking the editOptionButton, I get the following stack trace
Process: com.company.app, PID: 20916
android.content.ActivityNotFoundException: No Activity found to handle Intent { (has extras) }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2007)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1673)
at android.app.Activity.startActivityForResult(Activity.java:4586)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
at android.app.Activity.startActivityForResult(Activity.java:4544)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754)
at com.company.app.adapters.OptionsAdapter$1.onClick(OptionsAdapter.java:86)
the context that calls startActivityForResult is the MainActivity and is not null and alive (visible) at the time of calling because the adapter runs in it.
Therefor, I have no idea why this error pops up nor what I could do about it. Do any of you may know why or have experienced it before?
You have to include the Activity name like this
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra("option",o);
context.startActivityForResult(intent,1);
First
you are initlizing the intent in wrong way, you need init it as below:
Intent intent = new Intent(context, SecondActivity.class);
Second
If the result do not return to your MainActivity then you need to cast the context to activity before starting the second activity as:
((Activity) context).startActivityForResult(intent,1);
This means that intent doesn't know where to go. So give the context and the activity name where u want to go.
Intent intent = new Intent(getActivity(), SecondActivity.class);
intent.putExtra("Key",value);
startActvity(intent);

sending a data to another activity with putExtra and doesn't work java android

I used intent to send a primarykey of my app to another activity but it debug my application I don't know why?
I'm sure that I put the syntax corrrectly and I tried many way I used bundle object but the same result the application has stopped
Activity iden_espvol
Intent intent=new Intent(iden_espvol.this,verf_tel_espvol.class);
intent.putExtra("TEL",tel.getText().toString());
startActivity(intent);
finish();
Activity verf_tel_espvol
Intent intent = getIntent();
String sent=intent.getStringExtra("TEL");
Try this code.. and i hope you define both activity in manifest file..
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent=new Intent(LayoutActivity.this,MainActivity.class);
intent.putExtra("TEL","65421321");
startActivity(intent);
finish();
}
});
and in second activity onCreate() method
Intent intent = getIntent();
String sent=intent.getStringExtra("TEL");
Log.d("Data",sent);
make sure value get only oncreate or onResume method because first activity finished that time call second activity this method.

onbackpressed() from Activity to Specific Fragment

I have a MainActivity class with 3 Fragments (each in their own class)
My third fragment (LoginFragment) will allow Login a user and then go to a new activity (new Intent) with some info for that user like the product.
If I press back on that Intent will go back to the LoginFragment.
I override the #OnBackPressed to start the MainActivity:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
this.finish();
I need to know how to replace that Fragment with the LauncherFragment (Fragment 1) in MainActivity.
I have this solution but it takes 0.5 sec to 1-2 sec based on device
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
this.finish();
would be cool to go direct to Fragment 1 like to finish the third fragment thanks :)
I fixed the issue in this idea
onBackPressed() I call a new Intent but with extras
In the MainActivity that has the 3 fragments onRestart() I check if it coming from this class ( has that extras ) than go to this fragment (click,replace,delete)
#Override
public void onBackPressed() {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra(Constants.Intents.NAVIGATE_BACK, true);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
this.finish();
}
on the MainActivity I got this
#Override
protected void onRestart() {
super.onRestart();
Intent intent = getIntent();
boolean navigate = intent.getBooleanExtra(Constants.Intents.NAVIGATE_BACK, false);
if (navigate) {
View homeView = bottomNavigationView.findViewById(R.id.home);
homeView.performClick();
intent.removeExtra(Constants.Intents.NAVIGATE_BACK);
}
}
If you want it to be as fast as possible, use one activity and fragments to vary the contents. Adding a fragment doesn't create a new window, whereas an activity does.
Also look at your application logic in fragment / activity startup (onCreate(), onResume(), etc). That's going to be the main factor.

Android - New Intent starts particular method

I want to start one of my existing activities and force the activity to call a specific method after it starts. Is this possible?
Can I define a method that should be called after creating the activity inside my Intent?
For example, something like:
Intent intent = new Intent(this, com.app.max.Home.class.myMethod);
No, I don't think you can have something like this:
Intent intent = new Intent(this, com.app.max.Home.class.method);
but you can do this:
Intent intent = new Intent(this, com.app.max.Home.class);
intent.putExtra("methodName","myMethod");
startActivity(intent);
and then in the called activity (where you need to start the method), you can take the intent and decide which method to call like this:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if(intent.getStringExtra("methodName").equals("myMethod")){
mymethod();
}
}
Hello You can't call a particular method from intent but alternately you can use a service from intent and your requirement will be done.
Intent intent = new Intent(this, MyService.class);
and in MyService.class
public class MyService extends Service {
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
yourParticularMethod(); //do your task and stop the service when your task is done for battery saving purpose
context.stopService(new Intent(context, MyService.class));
return super.onStartCommand(intent, flags, startId);
}
#Override
public IBinder onBind(Intent intent) {
return null;
}
}
and register this service in your manifest file like this
<application>
<service android:name=".MyService" />
.
.
</application>
I solve this issue by using onCreate instead of onNewIntent.
Activity A:
Intent intent = new Intent(this, com.app.max.Home.class);
intent.putExtra("methodName","myMethod");
startActivity(intent);
com.app.max.Home Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
if(savedInstanceState == null)
{
Bundle extras = getIntent().getExtras();
if (extras == null)
{
//Extra bundle is null
}else{
String method = extras.getString("methodName");
if (method.equals("myMethod"))
{
//Call method here!
}
}
}
Hope this solution solve your problem
You question seems interesting, but there is no way you can do it using Intent. You have to understand that when you start an activity, it goes through a life cycle which is : onCreate()->onStart()->OnResume(). So what you can do is start that method from onResume() like this:
#Override
protected void onResume() {
super.onResume();
myMethod();//start your method from here
}
I'm just trying to help,give me some more information about your problem if this approach does not solve your problem.

Android: How do I call an activity when service is destroyed (onDestroy)?

Does anyone know how to call an activity when a service is destroyed? I have tried with the code below, but it doesn't work and geves me a force close.
#Override
public void onDestroy() {
Intent goToAbout = new Intent(this, AboutForm.class);
startActivity(goToAbout);
}
Anyone have an idea?
public void onDestroy(){
Intent myIntent = new Intent(this,
MyACtivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(myIntent);
}

Categories

Resources