Opening A Specific Activity based on parameters in a URI - android

I'm using Xamarin.Android to build an app for a customer that's meant to be supplementary to another app. The idea is that once they reach a point in their workflow where my app steps in, they will click a button inside their app that will launch my app, and will open an activity from my app based on some attached parameters.
My problem is that in the "calling" app, all that's exposed for me to work with is a parameterized URI that's passed directly to Android, so I have no means of creating and passing an Intent object. I can change the URI it sends to be whatever I like, so I've already added an intent filter that looks for the custom scheme myapp://.
I am new to SO, so I apologize if this question has been answered elsewhere, but I have looked for a few hours and in my searches so far, all I've seen are answers related to how to call another app from my own. The question I have is... how do I parse that request on the other end and know not only which activity to open, but the rest of the data or parameters that were in the URI? Is it possible to open an activity based on a parameter in that URI, or can I only point it to one activity?
Thanks in advance!

What you have to do is quite simple in your URL somewhere pass your activity name that you want to navigate to
Then in your code maintain a enum with all the possible activities you wanna navigate to:
enum ActivityName
{
MainAcitivity,
SomeotherAcitivity
}
Then in your received notification get the string where you have given the name of the activity and do something like this :
If(youractivityName==ActivityName.MainActivity.toString())
{
StartActivity(typeof(MainActivity));
}
Revert in case of queries.

Related

Passing data to other activity without opening it

I'm trying to pass the data from one activity to another activity without opening the activity. I tried it by not writing startActivity(intent) part but it fails.
How do I pass the data from one activity(1) to another activity(2) without opening that activity(2)?
Activities are UI elements so your question makes no sense. If you want perform some action that does not have UI create a service to perform the work and start or otherwise call that service from your first activity.
If you want to make data available to activity 2 when it does start, pick a persistent storage mechanism and write the data there, then read the data when activity 2 opens.
https://developer.android.com/guide/topics/data/data-storage
Alternatively, you can create a custom Application and store the data there and share it between activities.
Just a word of advice. You should describe what you are trying to accomplish not how you want to accomplish it. Activities are not the right tool for this task, but we can't suggest the right tool because we don't know what you want to build.
You can use events for passing around data. Either use the android native LocalBroadcastsSytem and put data as parcelable in the intent, or you can use any event library like EventBus
Pass the data from one activity to another activity without opening the activity try to do with the broadcast receiver or SharePhreferance or make that data public static You can access your data in anywhere of the application.

Storing activities in an array

I am developing a quiz app in which when the user presses the next button , he gets to a different activity that is the next question . Also it will have a previous button which takes to the previous question . So what I am thinking is to store all the activities in an array(if thats possible) . Later when I need to add or remove the question from the app then I will only have to change the array and delete its activity file .
That is a really bad idea. Instead, in the activity where the user chooses the question activity, pass data through the intent and deal with the logic in the receiver activity itself. Like so:
Intent j = new Intent (this,
QuestionsActivity.class/*whatever your activity's name
is */);
j.putExtra("questionNumber",13/*Arbitrary number*/);
startActivity(j);
Then in the activity....
if (getIntent().getIntExtra("questionNumber") == 13)
//Do what you want here
You should never, ever maintain references to multiple Activities. The Android system may decide at any time to destroy an Activity and your references will no longer be valid.
Instead you should separate the model of questions from the way they are displayed in an Activity. You can send data to an activity, for example some identifier for the question or even the text of the question. Your Activity subclass should be general-purpose in that it can display any question. This allows you to create one activity for all questions rather than one activity for each question.
You can keep an array of questions rather than activities and then use that array to display the correct information as needed.

Two SearchView components with different searchable configuration

Hi I have problem with planning implementation of specific search interface.
I am making navigation application where you can navigate from point to point.
For this I need two seachviews both with different searchable configuration, where I can define different custom intent action to recognize if user enter source or destination and react properly. I have already created suggestion logic because I am using one SearchView in other activity and already have different configuration there.
So my plan is creating an activity for result which will be in same time SearchActivity where I will setup two different SearchViews components where based on user input there will appear suggestions and after clicking them (first later second) the intent will be picked by CUSTOM1/2 ation intent in onNewIntent() as it will be "singleTop" activity and stored properly. Then i will process stored source&destination data and finish with proper response code to the source activity with extras based on processed data.
I don't know if I explained it properly. The main problem for me here is that I need to have two different SeachViews widgets. How to achieve this?
Eventually I can manage both SearchView suggestion results with default ACTION_VIEW and recognize them by sent data but I think the first plan is more clear.
PS I am planning to put them directly to the layout without appbar.

Better way to retain/pass data between activities

I am making an Android app using the Parse SDK. What I am struggling with is the flow of creating a post. Currently, in my main activity, a user selects the type of post (photo, video, etc.) takes a photo/video and goes to the next activity called NewActivity. In this activity, a user can review the photo/video and edit the privacy or place of the post. to change the privacy or place launches a new activity for each.
The main problem I'm having is retaining and passing this Post object between the activities.
My first (bad) solution was just to pass the data with the intent in a Bundle, but this soon got very messy as I really needed to pass a Post object between the activities. I switched from that solution to using a Singleton class called, DataHolder.
In each activity, I call DataHolder.getInstance() and when the create post button is clicked, I create a new Post object by executing: DataHolder.getInstance().setPost(new Post()). In the following activities, as the user enters more information about a post, I set the Post's properties.
This was all working well until I ran into this issue. When I would return to my app (presumably it had been killed) I would get a NullPointerException because the Post object was null. I was looking through the Android docs on passing data between activites/services and needed a little help.
Should I be using the Singleton class pattern here? What would be the most efficient and easiest way to pass this Post object between the activities? Should I use an application singleton? I would use Parcelable or Serializable, but the Postobject is a ParseObject so this is not an option. Should I avoid passing the data altogether by using Fragments for the privacy and place activities (though they have different screens and different action bars)? Should I use startActivityForResult for the privacy and place activities?
You should consider that the app could be killed at any moment for any reason and be prepared for this. You could use something like a session object to communicate between activities. You can save it / load it as often as needed so you will not run the risk of loosing data. If you cannot serialize the actual object (eg the ParseObject) you could serialize the parts needed in order to reconstruct it (ie save the image in a temp folder and load it on demand).

How to call MyStringVar.class

First of, sorry for the bad title, I couldn't think at all what to call this.
Say if I have a string with a value of "ActivityMain", and I have a Activity in my project called ActivityMain. Is there anyway I can get a new instance of the class by the string?
The overall idea is to request data from a server, what sends back some different Activity classes, then I want to start whatever activity is returned.
Assuming you can get the fully qualified name for the activity's class, you could follow this answer and use:
Class<?> act = Class.forName("com.bla.TestActivity");
I think if else if ladder may do what you want.
if(responsefromsever.equals(NameOfActivityInString)){
// instantiate the activity
}
use case:
I am assuming there exist a Activity class whose name is MainActivity. And what you receive from server is response, (i.e String)
String nameOfActivity = "MainActivity";
if(response.equals(nameOfActivity)){
MainActivity instantiation or whatever you want to do
}else if(response.equals("SomeOtherActivity")){
//SomeOtherActivity or whatever you want to do
}
The overall idea is to request data from a server, what sends back some different Activity classes, then I want to start whatever activity is returned.
A simple way hat is coming to me is that you can have some switch condition or if-then-else conditions which would compare the string received and accordingly start the desired activity. Eg:
if ( stringReceived.equals("ActivityMain"){
//start ActivityMain
} else{
//others...
}
This might be useful, if there are not many activities to start.
It seems like you are sharing implementation details to your server of your client program. What if you want to hook up the server to an iOS app? Then the whole activity name returning idea wouldn't work. "Drawing a different line" for your interfaces might be the best option.

Categories

Resources