Im working on my level-selector activity right now and I want to get the result from wich level I chose in my MainGameView, that is run by another activity. How would I go about doing this?
This is what Ive for when you choose level 1:
Button test1 = (Button)findViewById(R.id.test1);
test1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
setLevel(1);
Intent start = new Intent("nielsen.happy.activities.HAPPYACTIVITY");
startActivityForResult(start, getLevel());
finish();
}
});
but where do I go from here? How do I get this value into my view so I can change bitmaps and other values depending on what level they chose? Is there something I need to do in my "HappyActivity"(MainGameAcitvity) to get the result? cus right now its returning 0
explenation of how games set up: Menu -> levelselector -> Game. So I need to get result from levelselector into game, so it knows what level to start.
(updated corrected response)
Don't use startActivityForResult().
Try this for Activity A:
Intent start = new Intent("nielsen.happy.activities.HAPPYACTIVITY");
start.putExtra("level", getLevel());
startActivity(start);
finish();
Then in Activity B, do this:
Extras mBundle = getIntent().getExtras();
int level = mBundle.getInt("level");
(original, incorrect response)
Lets call activity A the one that houses your sample code above.
Lets call activity B the one that you refer to as HAPPACTIVITY.
Activity A calls startActivityForResult() with a request code.
Activity B starts up and before exiting, you call setResult(int code).
When activity B finishes, A returns to the top via the method onActivityResult().
Implement an onActivityResult() and see what attributes you get.
FYI there is a condition where setResult(RESULT_OK) or setResult(RESULT_CANCELED) will not trigger onActivityResult() in A (I can't recall what the case is).
Related
I want to make a app quite similar to contacts app. In the first activity, I want to show my contact list and I have done this. First Activity consists of add new button in which on the click of that button, I want that user enter first and last name and upload an image for the same. I don't know how to navigate between activities so that i get the required answer. Can anyone please help me..??
See the first Activity at: http://postimg.org/image/996iwj5dp/9c92bd46/
and Second Activity at: http://postimg.org/image/wuscpv1tx/fe6f13c4/
on Second activity there is also a button which will make user able to choose a picture from Gallery and thats acts as 3rd Activity. I get confused in getting data from these activities.
You need to implement startActivityforResult() method in first activity.
Follow below steps to perform the operation:
1) On click of the Add new button in your First activity, instead of calling startActivity(), call startActivityforResult() using a unique activity code:
i.e: public static final int FIRST_ACTIVITY_CODE = 0;
In OnClickListener, you need to implement,
Intent m_data = (FirstActivity.this,SecondActivity.class);
m_data.putextra(//any extra data);
startActivityforResult(FIRST_ACTIVITY_CODE,data);
2) Now when you have added another user from the second activity and you want to pass some data from SecondActivity to FirstActivity, then follow below procedure:
Before you finish the SecondActivity after saving the data,
Intent m_data = getIntent();
m_data.putExtra(//data to pass in FirstActivty);
m_data.putExtra(//data to pass in FirstActivty);
.
.
.
setResult(RESULT_OK,m_data);
finish();
3) In the onActivtyResult() of the FirstActivty, do below procedure:
#Override
protected void onActivityResult(int p_requestCode, int p_resultCode, Intent p_data)
{
if(p_requestCode == FIRST_ACTIVITY_CODE && p_resultCode == RESULT_OK)
{
NewData = p_data.getExtras().get(//your data);
// update data
}
super.onActivityResult(p_requestCode, p_resultCode, p_data);
}
Hope this helps you.
So, here's the scenario.
There is one EditText which has its own TextWatcher set, used for setting the word count.
I also have a Navigation Drawer, Sliding, and in that have an option which launches a new Activity for result.
The result I want is the Target number of words the user wants to achieve, and then get the result from the user input and calculate the perecentage of target recived and set it to the text of a TextView in the Main Activity.
Now, the problem is :
Navigation Drawer has it's own ItemClickListener, and it exists as an independent View in the Activity. (Hidden, mostly, that is.)
And the Main Activity is different view.
How can I implement a correct OnActivityResultMethod so that I can return to the Activity's oncreate Method, techinically speaking, to the TextWatcher so that the calculation can be made and percentage be set.
Because the OnActivityResult is called automatically, so I cannot do anything to override it.
It has the data the app needs, but it is not called progmatically, so it cannot return values.
You can try this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
Intent i = getIntent();
i.putExtras(data.getExtras()); // pass result data to onCreate()
startActivity(i);
finish();
}
Restarting the Activity is the only way to get back to onCreate().
EDIT:
Now, in your onCreate(), make a check:
Intent i = getIntent();
Bundle args = i.getExtras();
if(args != null){
....
}
and handle the data in the EditText here.
If you launch the Activity with an startActivityForResult() you can get the result very easily with an onActivityResult().
Check the code in this post and try it out.
How to manage `startActivityForResult` on Android?
Having trouble passing data between activities.
ListActivity is collecting data and when back button is pressed returns to MainActivity and then want to get that data via onResume method but I don't get anything.
How can this problem be solved?
ListActivity.java
#Override
public void finish() {
i = new Intent(ArrayListActivity.this, MainActivity.class);
i.putParcelableArrayListExtra(Constants.TAG_SELECTED_PRODUCT_LIST, selected_list);
super.finish();
}
MainActivity.java
#Override
protected void onResume() {
super.onResume();
Bundle extras = getIntent().getExtras().getBundle(Constants.TAG_SELECTED_PRODUCT_LIST);
if(extras != null) {
selected_list = extras.getParcelableArrayList(Constants.TAG_SELECTED_PRODUCT_LIST);
myListView.setAdapter(new ProductAdapter(MainActivity.this,
R.layout.array_lisviewt_item_row, selected_list));
}
}
You probably want to start your second activity from the first one via the startActivityForResult(...) method.
This method allows you to transport results from a launched activity back to it's launching activity.
From the documentation:
Launch an activity for which you would like a result when it finished.
When this activity exits, your onActivityResult() method will be
called with the given requestCode. Using a negative requestCode is the
same as calling startActivity(Intent) (the activity is not launched as
a sub-activity).
You'll want to start the activity with startActivityForResult() and override onActivityResult() to handle the data you return from the second Activity.
Check out this article on the Android developers site for more info.
Perhaps you should
Call your ListActivity with startActivityForResult(), from your MainActivity.
Once you finished working on the ListActivity, you call setResult() to set the data, followed by finish().
This will bring you back to your previous MainActivity. So how do you retrieve the data set by your ListActivity?
In MainActivity, override onActivityResult().
There's a brief explanation of this mechanism in Starting Activities and Getting Results.
How to switch layouts? First, I have a class Main where is onCreate (setContentView(R.layout.main);) and then I call, another class with command:
setContentView(secondClass);
In this class, I draw with Canvas and this work just fine. I also create button to go back in first "class" (R.layout.main), but I don't know how to do it.
Now my program is basic a graph shower. In first class you type your function and them second class draw it. But how to go back in first class to type another function. This "back" button or arrow witch every Android phone have, send me out of program not back on insert part.
In secondClass I can't create onCreate method, but I also tried the following and they didn't work:
Intent abc = new Intent("bla.bla.bla.FIRSTCLASS");
startActivity(abc);
and
Intent abc = new Intent(SecondClass.this,FirstClass.class);
startActivity(greNaPrvoOkno);
If you want to use a custom view (as I understood, you are extending the View class), you can do it in the following way;
Consider you are showing the second class from your Main activity like this;
setContentView(new SecondClass(getApplicationContext(), MainActivity.this));
And you Second class is this (suppose);
// I am using onClickListener to go back to main view. You do whatever you like.
public class SecondClass extends View implements OnClickListener {
// This is needed to switch back to the parent activity
private Activity mParentActivity = null;
public SecondClass(Context context, Activity parentActivity) {
super(context);
mParentActivity = parentActivity;
setOnClickListener(this);
}
#Override
public void onClick(View v) {
// Set the Main view back here.
mParentActivity.setContentView(R.layout.main);
}
}
Disclaimer: This code will do what you have asked for, but may cause other problems.
As advised by #Mudassir, you should use two different activities for two screens. It will give you better control, and your code will be easy to understand and maintain.
On the Onclick event of the button you have to write finish(); that's it..
Both of your classes are Activities yes? IF so then in your second activity you will simply call finish() and your activity will close revealing your first activity again.
When I have used multiple intents in my android application, I have created a new activity through:
Intent abc = new Intent(this, SecondClass.class);
startActivity(abc);
When the button is pressed in your second class, I would then either call finish(); on the class, or create a new intent like so:
Intent abc = new Intent(this, FirstClass.class);
startActivity(abc);
However, this method has the disadvantage that if a user wanted to use the back button, they may have to scroll through many layers of activities.
You should create another activity for your second class but not just set the main activity to a new view setContentView(secondClass).
For an easier modification, You could try to set the view back to setContentView(R.layout.main) first.
You still need to configure the widgets(e.g. TextView) on it when you set it back.
You don't have to startActivity again to go back.
Just call finish() in your second activity when you want to finish the current activity and go back:
e.g. When user press the back button in your second activity
mButtonBack.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
finish();
}
}
I need help doing something that I am sure is simple, but I can't figure out how to do it. I have a counter down and when it gets to the last 60 seconds, it calls a 'lastminute' counter activity. The plan is to overlap the last 60 seconds over the actual application.
Here is the problem, how can I change the code to allow the two activities to start at the same time.
I have tried this;
public void onFinish() {
startActivity(new Intent ("eu.merso.phoneapp.LASTMINUTE"));
startActivity(new Intent ("eu.merso.phoneapp.DASHBOARD"));
onDestroy();
}
but this does not put both applications on the screen, what I want is DASHBOARD on the background and LASTMINUTE on top. LASTMINUTE is alreay a "transparency colour".
Thanks;
Ramón
It won't work the way you're currently trying to do it. There can only be one visible activity at a time.
You should first start the dashboard Activity and from there you should start lastminute.
Edit --
Use a Bundle object.
Bundle bundle = new Bundle();
// Use 0 when the activity is called by the button and
// 1 when it is called by the timer.
bundle.putInt("event_src", 0);
intentObject.putExtras(bundle);
// In your new activity you can then check whether to display
// the countdown or not
Int eventSrc = getIntent().getExtras().getInt("event_src")
You need to implement the lastminute functionality in a dialog that you create and show in the onCreate method of your dashboard activity.
EDIT:
To distinguish between which activity that starts the new activity, use intent extras:
//in your calling activity
Intent i = new Intent(A.this, B.class);
i.putExtra("from Activity", A.class.getSimpleName());
startActivity(i);
//in your receiving activity
String from = getIntent().getStringExtra();
if(from.equals(A.class.getSimpleName())){
//do something
}
else if(from.equals(C.class.getSimpleName())){
//do something
}
Try using android:theme="#android:style/Theme.Translucent.NoTitleBar in Activity attributes for LastMinute in AndroidManifest.xml. I hope it'll be productive.
I think using Android fragments will help u to show two separate activities in the context of another main activity.
try reading this:
http://developer.android.com/guide/components/fragments.html