Xamarin Unable to create Intent - android

I followed the Xamarin tutorial to open another activity but I receive an error message saying: the type or namespace name"Intent" could not be found(are you missing a using directive or an assembly reference?)
enter image description here
May I know if there is anything missing or wrong in my code? Thank you

Make sure you have referenced using Android.Content; on top of your activity

You Need code something like this ...
home.setOnClickListener(new View.OnClickListener() { //here home is a button
#Override
public void onClick(View v) {
Intent intent=new Intent(LoginOption.this,HomeActivity.class);
startActivity(intent);
}
});
You Must need two activity first where your butoon exist, to open another activity Intent intent=new Intent(LoginOption.this,HomeActivity.class); here first activity is loginoption activity where my button is exist and another home activity which will be open after clicking event.

Related

How to switch to a previous activity in Android without error message?

I have 2 screens corresponding to 2 activities on my Android app. I am trying to go back to the first activity from the second screen by clicking a Back button. Even though my first activity restarts, I get an annoying pop-up message "Unfortunately the app has stopped" as my app refreshes to go back to the first activity. I would like to not have this message appear.
I have tried different variations of code and the message appears each time :
#Override
public void onBackPressed() {
startActivity(new Intent(this, MainActivity.class));
}
or
public void finishActivity(DisplayMessageActivity v){
Intent intent = new Intent( this, MainActivity.class );
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
or even just
public void finishActivity(DisplayMessageActivity v){
finish();
}
You should read the logs to find out what the exception is and why it is occurring. Places to check:
If back navigation logic is inside an OnClickListener, something inside the OnCLickListener code is probably null.
The activity it should go to after pressing back is having a null value for something you're trying to use.
Any asynchronous calls from old activity finishing after it has been removed.
Your log should tell you exactly what it is.

Using life cycles in android

I'm a new android programming beginner :) .
I have made a simple app which adding places .
1- Main activity include buttons{Places , Add place , Favorites Places }
so let's assume we are in the "Add place" activity , we type tite and description and add the place to arrayList, and move on to Places which has this list ..
The problem is when I type back, I get back the details I wrote about the place, but I'm trying to clear it when I press back in the phone ...
how can I do this to avoid crashes ?
Thanks all
-- Update :
Thanks to all people who reply trying to help me, that's awesome :)
I just added :
#Override
public void onBackPressed(){
Intent intent = new Intent(PlacesActivity.this,MainActivity.class);
startActivity(intent);
}
This solution was suggested by : Arsen Sench :)
in the activity that I don't want to press back,
You can do one thing here.
Before jumping to another activity View Places , just clear all the edittext there.
edittext.setText("");
Try this. Will help you.
just clear the EditText field before you start a new activity
editTextTitle.setText("");
editTextDescription.setText("");
intent.startActivity();
You can use startActivity() from both classes. Or you can use startActivityForResult(). Here is example of using startActivity() from both activites.
From first Activity
Intent i = new Intent(YOURFIRSTACTIVITY.this,YOURSECONDACTIVITY.class);
startactivity(i);
finish();
From Second Activity
#Override
public void onBackPressed()
{
Intent i = new Intent(YOURSECONDACTIVITY.this,YOURFIRSTACTIVITY.class);
startactivity(i);
finish();
}

Loose coupling and starting activities

I want to keep my app as loosely coupled as possible, and most of is done with IoC
however, at some point, i need to launch different activities,and the class implementing this activity, could be any,meaning i don't want to define a specific class that starts the activity, but one needs to be set in the intent.
where is the best place to write the code to launch my activities ? is it in the same activity that starts the other activity? or have some outside logic about it?
I have an activity A
from which i need to start activity B
where do i put the logic of
Intent intent = new Intent(this, B.class);
startActivityForResult(intent, requestingB);
It sounds like you're trying to have an activity that can be launched by some other application and you don't want the activity to necessarily know about what is launching it.
Try using an intent-filter within your activity. Then, when something needs to launch it, all it has to do is fire off an intent with the action defined in the intent-filter.
As always, Vogella has a good tutorial here: http://www.vogella.com/tutorials/AndroidIntent/article.html
As the OP mentioned in comments that he wants to start another activity on the click of a button, below is the sample code:
Button myBut = (Button) findViewById(R.id.but1);
myBut.setOnClickListener(new onClickListener()
{
#override
public void onClick(View view)
{
Intent intent = new Intent(A.this, B.class);
startActivity(intent);
}
});
Hope this helps

Android, Calling Activity within itself

I am trying to make an app which have 2 main controls (along with other info fields)
1.> Next button
2.> Done button
I want to call the same activity when next button is pressed and display some other activity when Done button is pressed
Done button is working fine. But when I press Next button the app stops working. The error that I get is :
Unfortunately, myapp has stopped working
This is the same error which I usually get when I don't define activity in manifest file.
Can anyone please help me with this problem.
And finally Is is legitimate to call same activity within itself ?
Thanks
I think this should work
Intent i= new Intent(ActivityA.this,ActivityA.class);
You can use Intent flags to call the activity again. In the button click
setContentView(R.layout.main);
Intent intent= new Intent(this,SameClass.class);
startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
Try this
Intent intent= getIntent();
finish();
startActivity(intent);
Thank you
Why do you need to call the Activity within itself?
You can do following things:
1. You can reset data on next button click.
2. You can hide view or make visible on next button click.
Clear your requirements and show your code to check why the error Unfortunately, myapp has stopped working is coming.
Use TabGroupactivity
In the next button onclick write
public void next(View v) {
Intent next= new Intent(getParent(), next.class);
TabGroupActivity parentActivity = (TabGroupActivity) getParent();
parentActivity.startChildActivity("next", next);
}
Intent i= new Intent(ActivityA.this,ActivityA.class);
startActivity(i);
or
startActivity(new Intent(ActivityA.this,ActivityA.class));

How to connect GUI screens in Android ?

How to connect GUI screens in Android? I have created several screens and I want to connect these using buttons.
It depends what else you want to do by clicking a button. If you only want to click a button and go to next activity or you want to send some data to next activity. Or maybe you want to dial a number or view contacts etc.
Here's a nice tutorial about using startActivity() and startActivityForResult() methods - android startActivity and startActivityForResult
Also basic Intents Tutorial will be useful.
just take a event handler .. in that use INTENT
Here s the syntax , startActivity(Intent(activity1.this, activity2.class);
ex:
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(activity.this, activity2.class);
startActivity(i);
}
});
Here !st activity1 i..e.. ur current class(1st page) name
Intent is the best mechanism to connect your component(activties, services) run time. here you can find about intent class.

Categories

Resources