How to show a new screen from menu item - android

I'm new to android and trying to find out how to show a new screen when the user clicks something in the menu item.
I'm using ActionbarSherlock and looking at the sample github-android app.
When the user clicks on an item in the menu, I want to show them a new screen. Github code is doing that like so:
startActivityForResult(new Intent(getActivity(), CreateGistActivity.class), GIST_CREATE);
But I've seen some code samples do:
Intent i = new Intent(getApplicationContext(), SomeActivity.class);
My code looks like this:
public class MainActivity extends SherlockActivity {
....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.create) {
//show createactivity class
return true;
}
return true;
}
What is the right way to do ?

You can do it just like that:
public class MainActivity extends SherlockActivity {
....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.create) {
//show createactivity class
Intent i = new Intent(MainActivity.this, SomeActivity.class);
startActivity(i);
return true;
}
return true;
}
startActivityForResult is used when you have to return some value/data to the first screen like a user selection. More here
As far as the context to use getActivity() or getApplicationContext(), I prefer to use the context of current activity MainActivity.this its more straitforward similar to documentation example
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
Inside a fragment use getSherlockActivity() instead of getActivity() as getActivity() can cause crashes to older devices.
Of course getApplicationContext() would always work and not crash but I feel that it may mess the garbage collector and do not let activities to be cleared (but not sure about it)

Just use startActivityForResult

There is no 'right' way. The Github code doesn't first declare the variable. The onther does. I believe for a menu, you normally need to declare the Intent as a local variable, if not a field.

Create an intent: Intent i = new Intent(MainActivity.this, CreateGistActivity.class);
where MainActivity is the activity you're in, and CreateGistActivity is the class you want to launch.
Then use startActivity(Intent) to launch the new activity: startActivity(i);
Or just combine them:
startActivity(new Intent(MainActivity.this, CreateGistActivity.class));
Full code:
public class MainActivity extends SherlockActivity {
....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.create)
{
Intent i = new Intent(MainActivity.this, CreateGistActivity.class);
startActivity(i);
return true;
}
return true;
}
startActivityForResult probably isn't needed in your case, unless you're expecting to send values between the classes.

Related

Know which item selected?

I have an application where you can select an item from a AlertDialog spinner like, but I don't know how to make my app, to memorize the selected choice, and then behave my button to this. This is what I have now:
#Override
public void onClick(View v) {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
mp = MediaPlayer.create(getApplicationContext(), R.raw.ais);
mp.start();
Intent myIntent = new Intent(MainActivity.this, Main2Activity.class);
MainActivity.this.startActivity(myIntent);
}
}, 9000);
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
startActivity(i);
}
});
}
And somehow to this code, I want to add my last selected item button, for example:
If ( last selected item from AlertDialog was = dance)
do the code, that I wrote above.
One way to do it is with an interface. Define an interface in your Alert Dialog fragment and instantiate it like this:
public interface MyDialogListener {
//put whatever data you want to pass as a paramenter, below I have two examples
public void onDataSelectedEvent( String action);
}
MyDialogListener myListener;
The listener will have to be instantiated in the dialog by typecasting your activity to a MyDialogListener.
You could do it like this:
myListener = (MyDialogListener) MainActivity.this;
Or even better would be to use the activity parameter in the dialog's onAttach(...) method.
We are able to do this because later, we will make your activity implement the MyDialogListener interface, which will effectively make the activity a MyDialogListener.
Put this line in the method or listener, maybe an onClick() in your alert dialog. This is for a spinner:
myListener.onDataSelectedEvent(mySpinner.getSelectedItem().toString())
Implement the interface in your Activity that receives the data like this:
public class MyActivity extends Activity
implements MyAlertDialog.MyDialogListener{
Then, in the activity receiving the data, have the interface method look like this:
#Override
public void onDataSelectedEvent(String action) {
//probably better to use a switch statement
if(action == "dance") {
// dance()
} else if(action == "stand") {
.......etc
}
}
There is a good example here: http://developer.android.com/guide/topics/ui/dialogs.html#PassingEvents

How to check if intent/activity is already open in Android?

So, I'm kind of a noob in Android but I searched a lot for this and I was not able to find a solution:
In my navigation drawer, each row opens a new intent. How can I check if a certain intent is open/active so that I'll use that instead of creating a new one?
I tried using this solution:
Link
But my issue is that the drawer opens the same class everytime, but each class has different "extras." For example:
public void itemClicked(View view, int position) {
Intent intent=null;
switch (position) {
case 1:
intent = new Intent(getActivity(), DisplayActivity.class);
intent.putExtra("ARGUMENT","SECTION 1");
break;
case 2:
intent = new Intent(getActivity(), DisplayActivity.class);
intent.putExtra("ARGUMENT","SECTION 2");
break;
case 3:
intent = new Intent(getActivity(), DisplayActivity.class);
intent.putExtra("ARGUMENT","SECTION 3");
break;
}
startActivity(intent);
}
How can I check if an intent with that class and with those extras is already open?
Thanks!
You can use shared preferences or extent from Application class where you store last/current activity visible
If you extend class for Application or you are targetting devices API Level 14 or above you can implement ActivityLifecycleCallbacks.
Sample Code
public class MyApplication extends Application implements ActivityLifecycleCallbacks {
private static boolean isMySomeActivityVisible;
#Override
public void onCreate() {
super.onCreate();
// Register to be notified of activity state changes
registerActivityLifecycleCallbacks(this);
....
}
#Override
public void onActivityResumed(Activity activity) {
if (activity instanceof YOURACTIVITY) {
isMySomeActivityVisible = true;
}
}
#Override
public void onActivityStopped(Activity activity) {
if (activity instanceof YOURACTIVITY) {
isMySomeActivityVisible = false;
}
}
// Other state change callback stubs
....
}
You can also check about the launch modes in android which you make use of.
I think it will serve your requirement well.
For your reference have a look at this
http://www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode

Changing from Activities to Fragments

I am trying to fix up my app code and transition from Activities to Fragments, in order to transition to the navigation draw and eventually have some sliding tabs too.
Currently for navigation I am using the 3 dots drop down menu from the action bar which I know is not really right. Which is why I am trying to transition to fragments.
I want to keep my action bar menu so I can keep my search icon in the action bar but I want to add a navigation draw to it.
I currentley have a class called ActionBarMenu which extends activity. All my activities come form this because this class holds all the functions which open new activities for the action bar menu I have set up right now.
public class ActionbarMenu extends Activity {
public void goToSearch (MenuItem item) {
// go to search page
Intent i = new Intent(this,Search.class);
startActivity(i);
}
public void goToStatistics (MenuItem item) {
// go to search page
Intent i = new Intent(this,StatisticsPage.class);
startActivity(i);
}
public void goToFindBrewery (MenuItem item) {
// go to search page
Intent i = new Intent(this,FindBrewery.class);
startActivity(i);
}
public void goToContact (MenuItem item) {
// go to search page
Intent i = new Intent(this,ContactPage.class);
startActivity(i);
}
public void goToPortfolio (MenuItem item) {
// go to search page
Intent i = new Intent(this,Portfolio.class);
startActivity(i);
}
public void goToDiscover (MenuItem item) {
// go to search page
Intent i = new Intent(this,Discover.class);
startActivity(i);
}
public void scanBarcode (MenuItem item) {
//open scanner
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
}
//get result value
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
// retrieve scan result
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null) {
// we have a result
String scanContent = scanningResult.getContents();
// launch async task for results
String url = "http://api.brewerydb.com/v2/search/upc?code=" + scanContent + "&key=0cdb22f84315834cc807c0176a927060&format=json&withBreweries=y";
new GetBeerDataFromUPCJSON(this).execute(url);
} else {
Toast toast = Toast.makeText(getApplicationContext(), "No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
}
I tried to change it to a fragment by changing extends activity to extends Fragment and all intents I build to launch new activities all get the red squiggly underline. I am just experimenting and trying to learn what's going on and whats the best way to transition my app from activities to Fragments, without having to recode everything.
Your problem is probably because in a line such as
Intent i = new Intent(this, Discover.class);
this now refers to a Fragment instead of an Activity. Intent() expects a Context as the first argument; Activity extends Context, but Fragment does not. To fix your problem, you should change this to getActivity().
For future reference, mousing over the little red x that appears next to the error should tell you exactly what the problem is.
Instead of using intents, you'll use the fragment manager to load new fragments. Read this page on the android website about fragments.
The finished code for quick replacement would be:
Intent i = new Intent(getActivity(), Discover.class);
startActivity(i);
you just need to make it same like backpressed if the activity is just next from your fragment like... it should work ....
#Override
public void onClick(View view) {
onBackPressed();
}
Intent i = new Intent(this, Discover.class);

Launching a new Activity from a Context Menu and automating another Activity in the onCreate relaunches the same Activity

I have two Activities in which I want to call the second one from the first one's Context Menu. This is what it should do.
Activity A Context Menu click should start Activity B.
In the onCreate of Activity B, depending on the extras passed in, automatically display a AlertBuilder dialog and then either take a picture or select an image.
What is happening is that when Activity A's Context Menu item is clicked, it launches Activity B and the AlertDialog displays. If I select the option to take a picture, the MediaStore.ACTION_IMAGE_CAPTURE intent is started and once the picture is taken, Activity B is re-launched again and the AlertDialog shows.
Activity A - Context Menu
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
final ItemClass selItem = (ItemClass)this.getListView().getItemAtPosition(info.position);
Intent intent;
SyncData sync;
switch (item.getItemId()) {
case R.id.start_activity_b:
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putExtra("data1", selItem.itemID);
intent.putExtra("data2", "AUTO");
Measurements.this.startActivityForResult(intent, REQUESTCODE_ACTIVITYB);
return true;
default:
return super.onContextItemSelected(item);
}
}
Activity B - onCreate Code
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.listview_main);
Bundle extras = getIntent().getExtras();
if ((extras != null) && (extras.containsKey("data1"))) {
this.itemID = extras.getString("data1");
}
if ((extras != null) && (extra.containsKey("data2"))) {
this.createAlertDialog();
}
}
As I stated in my comment, you shouldn't need runOnUiThread() since you are already on the UI Thread. There are a couple ways that may be better than doing this. Again, as in my comment, you should be able to call setResult and finish ActivityB when the Dialog dismisses since you are using startActivityForResult() to start ActivityB.
With just what I see that should be fine. But if it causes problems due to whatever you have in your Dialog or from something else, you can make your Dialog an Activity and give it aDialog themeby adjusting yourmanifest`. Use
<activity android:name=".ActivityName"
android:theme="#android:style/Theme.Dialog"> // add this line to give it the effect of a dialog
</activity>
You can start this Activity for result using startActivityForResult(). Then when that result returns to ActivityB you can finish and return to ActivityA or whatever you need to do.
Hope this helped.
I found the solution to it, after trying over and over to understand why. All that needs to be done is to create a runnable in the Context menu, as it seems to need to be returned quickly otherwise it recreates the Activity. I could be wrong and please feel free to correct me on this one.
Activity A - Context Menu
#Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
final ItemClass selItem = (ItemClass)this.getListView().getItemAtPosition(info.position);
Intent intent;
SyncData sync;
switch (item.getItemId()) {
case R.id.start_activity_b:
runOnUiThread(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putExtra("data1", selItem.itemID);
intent.putExtra("data2", "AUTO");
Measurements.this.startActivityForResult(intent, REQUESTCODE_ACTIVITYB);
}
});
return true;
default:
return super.onContextItemSelected(item);
}
}

Android: Launching new activity from dialog

I'm trying to use a custom dialog to get user input for a title String, and then if the user clicks OK, it will fire up a new Activity (basically a notepad) with that title String as the title. However, when I try to call the method that fires the new activity inside onClick(), it gives me an error.
Here's the code
class NewListDialog extends Dialog implements OnClickListener {
Button search;
EditText text;
public NewListDialog(Context context) {
super(context);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.newlist_dialog);
search = (Button) findViewById(R.id.dialog_confirm);
text = (EditText) findViewById(R.id.dialog_editable);
search.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == search) {
String title_name = text.getText().toString();
// method for launching new activity
fireListEdit(title_name);
}
}
}
void fireListEdit(String title_name) {
Intent i = new Intent(this, ListEdit.class);
i.putExtra(InvenDB.KEY_TITLE, title_name);
startActivityForResult(i, ACTIVITY_CREATE);
}
And I call this dialog with
#Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case INSERT_ID:
NewListDialog dialog = new NewListDialog(this);
dialog.show();
return true;
case QUIT_ID:
finish();
return true;
}
return super.onMenuItemSelected(featureId, item);
}
Edit: when I run it on emulator, when I click OK on the custom dialog it just gives me "the application stopped unexpectly" error
EDIT:
This is the logcat, I'm not really sure what they mean :\
01-12 17:39:27.668: ERROR/AndroidRuntime(426): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jin.inventoryapp/com.jin.inventoryapp.ListEdit}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
From what I can tell there is a problem with the Adapter (data) part of your application. Whatever Adapter class you used to back your com.jin.inventoryapp.ListEdit, it has a problem accessing the data. i.e. if you are using an array or database to populate your ListView, check there first.
This is useful to figure out who is throwing an exception.

Categories

Resources