Changing activities using phonegap on android - android

How do you change activities using DroidGap? Is there a way to get the current context even though we're using DroidGap?
public class MainActivity extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isOnline() == true) {
super.loadUrl("http://myurl.com");
}
else {
Intent myIntent = new Intent(SOME CONTEXT...DroidGap.getContext(), LoadScreen.class);
startActivity(myIntent);
finish();
}
}

This solved my problem Intent myIntent = new Intent(getContext(), LoadScreen.class);

Related

How to get last/previous intent to refresh

I am working on an online radio app demo. I've created an error Activity which I want to take the user to, when an error occurs. In the error page, there is a refresh button, which is supposed to refresh the last Activity where an error occurred. But I don't know how to get the Intent of previous Activity which led to the error page to get it refresh on ButtonClick, I only know to make it return to a particular Activity.
You can use startActivityForResult in both calling activities
In MainActivity.java
int REFRESH = 1;
private void startErrorActivity() {
startActivityForResult(new Intent(this, ErrorActivity.class), REFRESH);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REFRESH) {
//do refresh
}
}
And in ErrorActivity.java
Button button = findViewById(R.id.refreshButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish(); //this will take you back to calling activities onActivityResult method
}
});
UPDATE:
I honestly think #sneharc's answer is better. Use that.
Try this:
public class ActivityA extends Activity {
public void myFunction(){
try{
// something bad happens here. need to go to ErrorActivity
}
catch (SomeException e){
Intent startErrorActivityIntent = new Intent(this, ErrorActivity.class);
startErrorActivityIntent.putExtra("sourceActivity", ActivityA.class.getSimpleName())
startActivity(this, startErrorActivityIntent)
}
}
}
public class ActivityB extends Activity {
public void myFunction(){
try{
// something bad happens here. need to go to ErrorActivity
}
catch (SomeException e){
Intent startErrorActivityIntent = new Intent(this, ErrorActivity.class);
startErrorActivityIntent.putExtra("sourceActivity", ActivityB.class.getSimpleName())
startActivity(this, startErrorActivityIntent)
}
}
}
public class ErrorActivity extends Activity {
private Intent mReceivedIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
mReceivedIntent = getIntent();
}
public void onClickRefresh(){
String retryActivityName = mReceivedIntent.getStringExtra("sourceActivity");
Intent retryActivityIntent = null;
if (!TextUtils.isEmpty(retryActivityName)){}
if (retryActivityName.equalsIgnoreCase(ActivityA.class.getSimpleName()))
retryActivityName = new Intent(this, ActivityA.class);
if (retryActivityName.equalsIgnoreCase(ActivityB.class.getSimpleName()))
retryActivityName = new Intent(this, ActivityB.class);
}
if (retryActivityIntent != null)
startActivityForResult(this, retryActivityIntent);
}
}

AndroidRestart Activity from PreferenceFragment

I declared my PreferenceFragment within a SettingsActivity like this
public class ChordsSettings extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new MyPreferenceFragment())
.commit();
}
public static class MyPreferenceFragment extends PreferenceFragment {
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings_preferences);
}
}
}
I did it like this and didn't create a Fragment on top of the MainActivity because I need to be able to use the back button to get from the SettingsActivity to the MainActivity and this seamed like the only way to go to achieve that.
I need to restart my MainActivity once the preferences changed.
I tried sending a Broadcast from the preferenceFragment but sendBroadcast() cannot be used from a static context. Is there any other way I can achieve this?
Your MainActivity should probably look like this. Note that when btnGoToSetting is clicked finish() method is called. This is for closing the currrent Activity.
MainActivity.java
public class MainActivity extends Activity {
Button btnGoToSetting;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGoToSetting = (Button)findViewById(R.id.btnGoToSetting);
btnGoToSetting.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
}
});
}
}
SettingsActivity.java
public class SettingsActivity extends Activity {
Button btnSavePreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnSavePreferences = (Button)findViewById(R.id.btnSavePreferences);
btnSavePreferences.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Here is where you save all your preferences
yourSaveFunction();
finish();
Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
startActivity(intent);
}
});
}
#Override
public void onBackPressed() {
finish();
Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
startActivity(intent);
}
}
Note : There is another way to resfresh you data in MainActivity by making your data asynchronous so that everytime the data changes it will be refresh automatically.

getting force close when i pass intent botween two activities using setcomponentname cunstructor

Actually i am trying to implement you tube API in my android application .so here i am trying to start a activity that's extends a class which defined in same package from my Main Activity using intent but i am getting force to closed....
am giving my code bellow.....i hope anyone can help me and reply me immediately......thank you in advance
my main activity is :
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v)
{
Intent in=new Intent();
in.setComponent(new ComponentName(getPackageName(), youtubevideos.class));
startActivity(in);
}
you tube activity is:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View v)
{
Intent in=new Intent();
in.setComponent(new ComponentName(getPackageName(), youtubevideos.class));
startActivity(in);
}
`
try
Intent in=new Intent(this, youtubevideos.class);
startActivity(in);

neither onActivityResult( ) nor notifyDataSetChanged() work

I've read many answers on the same question but still I do not understand why my code doesn't work properly. I have a problem with (as I think) exchanging data between two activities.
I have 2 activities - the first contains ListView and an Add button.
When user presses Add new activity starts with the form to fill. When user completes the form he/she presses OK and my first activity starts again (it really does) and it should contain new item (but it doesn't).
This is my first activity:
public class DatabaseActivity extends Activity implements OnClickListener {
ArrayList<Student> students;
StudentDatabaseAdapter adapter;
ListView lvStudentList;
ImageButton imgBtnAdd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.database);
students = new ArrayList<Student>();
fillArrayList();
adapter = new StudentDatabaseAdapter(this, students);
lvStudentList = (ListView)findViewById(R.id.lvStudentsList);
lvStudentList.setAdapter(adapter);
imgBtnAdd = (ImageButton)findViewById(R.id.imagBtnAddStudent);
imgBtnAdd.setOnClickListener(this);
}
public void fillArrayList() {
//code here
}
#Override
public void onClick(View v) {
Intent intent;
intent = new Intent(this, WizardActivity.class);
startActivity(intent);
}
public void addStudent(Student student) {
students.add(student);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if(resultCode==RESULT_OK)
{
if(requestCode==2)
{
if (intent == null) {return;}
Student newStudent = new Student(intent.getStringExtra("name"), intent.getStringExtra("surname"),
intent.getStringExtra("last_name"), Integer.parseInt(intent.getStringExtra("year_of_birth")), R.drawable.default_ava);
addStudent(newStudent);
adapter.notifyDataSetChanged();
}
}
}
}
And that's my second activity:
public class WizardActivity extends Activity implements OnClickListener {
EditText etName, etSurname, etLastName, etYearOfBirth;
ImageButton imgBtnOK;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wizard);
etName = (EditText)findViewById(R.id.etName);
//initializing other edit texts
imgBtnOK = (ImageButton)findViewById(R.id.imgBtnOK);
imgBtnOK.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent(this, DatabaseActivity.class);
intent.putExtra("name", etName.getText().toString());
intent.putExtra("surname", etSurname.getText().toString());
intent.putExtra("last_name", etLastName.getText().toString());
intent.putExtra("year_of_birth", etYearOfBirth.getText().toString());
setResult(RESULT_OK, intent);
startActivityForResult(intent, 2);
}
}
There is nothing wrong with your notifyDataSetChanged(). You seem to be missing the way a secondary activity communicates results back to its caller activity.
DatabaseActivity.onClick() should call startActivityForResult() instead of startActivity().
WizardActivity.onClick() should just call finish() after setResult() (remove the startActivityForResult() call, it doesn't make sense there). Also notice that the intent you provide to setResult() can be an empty intent, i.e. Intent intent = new Intent();
After the secondary activity finishes, DatabaseActivity will be back to foreground and the result will be processed by DatabaseActivity.onActivityResult().
I think you haven't to start new activity in WizardActivity
try this :
WizardActivity
......
#Override
public void onClick(View v) {
Intent intent = new Intent(this, DatabaseActivity.class);
intent.putExtra("name", etName.getText().toString());
intent.putExtra("surname", etSurname.getText().toString());
intent.putExtra("last_name", etLastName.getText().toString());
intent.putExtra("year_of_birth", etYearOfBirth.getText().toString());
setResult(RESULT_OK, intent);
finish();
}
and in
DatabaseActivity
add if you want
#Override
protected void onResume (){
......
adapter.notifyDataSetChanged();
}

How to start Activity for result from IME

I'm developing an app that should return some text to the app that started the intent.
But the app that starts the intent is a IME/soft Keyboard. So StartActivityForResult is not available because an IME is a service.
How can I achieve this?
What I got so far:
Keyboard:
final Intent intent = new Intent("com.example.helloworld.GETTEXT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
intent.putExtra("keyboard", true);
startActivity(intent);
Other App:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle extras = getIntent().getExtras();
if (extras == null){
return;
} else {
finish();
}
}
#Override
public void finish() {
Intent data = new Intent();
data.putExtra("test", "PASSED");
setResult(RESULT_OK, data);
super.finish();
}
You can use ResultReceiver.
Look at this example, it's pretty clear explains how it works.
You could use a ResultReceiver for this is think.
ResultReceiver lReceiver = new KeyboardResultReceiver(aListener);
final Intent intent = new Intent("com.example.helloworld.GETTEXT");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
intent.putExtra(EXTRA_RESULT_RECIEVER, lReceiver);
intent.putExtra("keyboard", true);
startActivity(intent);
private static final class KeyboardResultReceiver extends ResultReceiver {
public FileUploadResultReceiver() {
}
#Override
protected void onReceiveResult(int aResultCode, Bundle aResultData) {
//Do your thing here you can also use the bundle for your data transmission
}
}

Categories

Resources